//package com.backendsys.service.Ai.Aizn;
//
//import cn.hutool.core.convert.Convert;
//import cn.hutool.json.JSONObject;
//import cn.hutool.json.JSONUtil;
//import com.backendsys.aspect.HttpRequestAspect;
//import com.backendsys.entity.Ai.Aizn.AiznGood.AiznGoodCompoundTask.AiznGoodCompoundTaskDTO;
//import com.backendsys.entity.Ai.Aizn.AiznGood.AiznGoodReplaceTask.AiznGoodReplaceTaskDTO;
//import com.backendsys.entity.Ai.Aizn.AiznImageTaskVO;
//import com.backendsys.exception.CustException;
//import com.backendsys.mapper.Ai.Aizn.AiznImageTaskMapper;
//import com.backendsys.service.System.SysResourceService;
//import com.backendsys.utils.MapUtil;
//import com.fzzixun.openapi.sdk.client.OpenClient;
//import com.fzzixun.openapi.sdk.common.RequestMethod;
//import com.fzzixun.openapi.sdk.request.CommonRequest;
//import com.fzzixun.openapi.sdk.response.CommonResponse;
//import org.redisson.api.RLock;
//import org.redisson.api.RedissonClient;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Lazy;
//import org.springframework.stereotype.Service;
//
//import java.util.LinkedHashMap;
//import java.util.Map;
//import java.util.concurrent.TimeUnit;
//
//@Service
//public class AiznGoodServiceImpl implements AiznGoodService {
//
//    @Lazy
//    @Autowired
//    RedissonClient redissonClient;
//
//    @Autowired
//    private HttpRequestAspect httpRequestAspect;
//
//    @Autowired
//    private AiznService aiznService;
//
//    @Autowired
//    private AiznImageTaskMapper aiznImageTaskMapper;
//
//    @Autowired
//    private SysResourceService sysResourceService;
//
//    /**
//     * [紫鸟] 商品合成 (发起任务)
//     */
//    @Override
//    public Map<String, Object> makeGoodCompoundTask(AiznGoodCompoundTaskDTO aiznGoodCompoundTaskDTO) {
//        RLock lock = redissonClient.getLock("makeGoodCompoundTask");
//        try { lock.tryLock(3, TimeUnit.SECONDS);
//
//            // -- 业务参数 -------------------------------------------------------------------
//            JSONObject object = new JSONObject();
//            object.set("imageUrl", aiznGoodCompoundTaskDTO.getImage_url());
////            object.set("style", aiznGoodCompoundTaskDTO.getStyle());
////            object.set("subType", aiznGoodCompoundTaskDTO.getSub_type());
//            object.set("ratio", aiznGoodCompoundTaskDTO.getRatio());
//            object.set("outputNum", aiznGoodCompoundTaskDTO.getOutput_num());
//
//            Map<String, Object> extendFields = new LinkedHashMap<>();
//            extendFields.put("imageSegUrl", aiznGoodCompoundTaskDTO.getExtend_fields().getImage_seg_url());
//            extendFields.put("keepSegStruct", aiznGoodCompoundTaskDTO.getExtend_fields().getKeep_seg_struct());
//            extendFields.put("segChangeWeight", aiznGoodCompoundTaskDTO.getExtend_fields().getSeg_change_weight());
//            extendFields.put("originalImages", aiznGoodCompoundTaskDTO.getExtend_fields().getOriginal_images());
//
//            object.set("extendFields", extendFields);
//
//            System.out.println("Params:");
//            System.out.println(object.toString());
//
//            // -- 实现方法 -------------------------------------------------------------------
//            OpenClient client = aiznService.getOpenClient();
//            CommonRequest request = new CommonRequest("/linkfox-ai/image/v1/make/create/11", RequestMethod.POST_JSON);
//            request.setBizContent(object.toString());
//
//            // -- 返回结果 -------------------------------------------------------------------
//            CommonResponse response = client.executeAppToken(request, aiznService.getAppToken());
//            JSONObject resp = aiznService.checkResponse(response);
//            if ((Integer) resp.get("code") == 200) {
//
//                // -- 成功 ------------------------------------------------------------------
////                Map<String, Object> respData = (Map<String, Object>) resp.get("data");
//                Map<String, Object> respData = Convert.toMap(String.class, Object.class, resp.get("data"));
//                System.out.println(respData);
//
//                // -- 将制作结果插入到数据表 -----------------------------------------------------
//                AiznImageTaskVO vo = new AiznImageTaskVO();
//                vo.setTask_id((String) respData.get("id"));
//                vo.setUser_id(httpRequestAspect.getUserId());
//                vo.setTask_type("GOOD_COMPOUND");
//                vo.setImage_url((String) respData.get("imageUrl"));
//                vo.setTask_submit_json(JSONUtil.toJsonStr(respData));
//                aiznImageTaskMapper.insertAiznImageTask(vo);
//                // --------------------------------------------------------------------------
//
//                // -- 扣除对应的本地积分 (ZnGood, GoodCompound, 商品合成) ------------------------
//                sysResourceService.consumptionPoint("ZnGood", "GoodCompound", null);
//
//                return MapUtil.convertToUnderscoreCase(respData);
//
//            } else {
//                System.out.println(resp);
//                throw new CustException((String) resp.get("msg"));
//            }
//
//        } catch (InterruptedException e) { throw new RuntimeException(e);
//        } finally { lock.unlock(); }
//    }
//
//    /**
//     * [紫鸟] 商品替换 (发起任务)
//     */
//    @Override
//    public Map<String, Object> makeGoodReplaceTask(AiznGoodReplaceTaskDTO aiznGoodReplaceTaskDTO) {
//        RLock lock = redissonClient.getLock("makeGoodCompoundTask");
//        try { lock.tryLock(3, TimeUnit.SECONDS);
//
//            // -- 业务参数 -------------------------------------------------------------------
//            JSONObject object = new JSONObject();
//            object.set("imageUrl", aiznGoodReplaceTaskDTO.getImage_url());
//            object.set("outputNum", aiznGoodReplaceTaskDTO.getOutput_num());
//
//            Map<String, Object> extendFields = new LinkedHashMap<>();
//            extendFields.put("denoiseStrength", aiznGoodReplaceTaskDTO.getExtend_fields().getDenoise_strength());
//            extendFields.put("sourceImageUrl", aiznGoodReplaceTaskDTO.getExtend_fields().getSource_image_url());
//            extendFields.put("targetImageUrl", aiznGoodReplaceTaskDTO.getExtend_fields().getTarget_image_url());
//
//            object.set("extendFields", extendFields);
//
//            System.out.println("Params:");
//            System.out.println(object.toString());
//
//            // -- 实现方法 -------------------------------------------------------------------
//            OpenClient client = aiznService.getOpenClient();
//            CommonRequest request = new CommonRequest("/linkfox-ai/image/v1/make/create/12", RequestMethod.POST_JSON);
//            request.setBizContent(object.toString());
//
//            // -- 返回结果 -------------------------------------------------------------------
//            CommonResponse response = client.executeAppToken(request, aiznService.getAppToken());
//            JSONObject resp = aiznService.checkResponse(response);
//            if ((Integer) resp.get("code") == 200) {
//
//                // -- 成功 ------------------------------------------------------------------
////                Map<String, Object> respData = (Map<String, Object>) resp.get("data");
//                Map<String, Object> respData = Convert.toMap(String.class, Object.class, resp.get("data"));
//                System.out.println(respData);
//
//                // -- 将制作结果插入到数据表 -----------------------------------------------------
//                AiznImageTaskVO vo = new AiznImageTaskVO();
//                vo.setTask_id((String) respData.get("id"));
//                vo.setUser_id(httpRequestAspect.getUserId());
//                vo.setTask_type("GOOD_REPLACE");
//                vo.setImage_url((String) respData.get("imageUrl"));
//                vo.setTask_submit_json(JSONUtil.toJsonStr(respData));
//                aiznImageTaskMapper.insertAiznImageTask(vo);
//                // --------------------------------------------------------------------------
//
//                // -- 扣除对应的本地积分 (ZnGood, GoodCompound, 商品合成) ------------------------
//                sysResourceService.consumptionPoint("ZnGood", "GoodReplace", null);
//
//                return MapUtil.convertToUnderscoreCase(respData);
//
//            } else {
//                System.out.println(resp);
//                throw new CustException((String) resp.get("msg"));
//            }
//
//        } catch (InterruptedException e) { throw new RuntimeException(e);
//        } finally { lock.unlock(); }
//    }
//
//}
