package com.inkasso.factoring.purchManagement.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.adapter.exception.BusinessException;
import com.inkasso.adapter.util.CodeUtil;
import com.inkasso.factoring.dict.service.DictionaryService;
import com.inkasso.factoring.file.entity.FileStorageDO;
import com.inkasso.factoring.file.enums.FileTypeEnum;
import com.inkasso.factoring.file.service.FileService;
import com.inkasso.factoring.purchManagement.dao.PurchSupplementContractSignConclusionMapper;
import com.inkasso.factoring.purchManagement.entity.PurchContractSupplementApproval;
import com.inkasso.factoring.purchManagement.entity.PurchSupplementContractSignConclusion;
import com.inkasso.factoring.purchManagement.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.xml.bind.ValidationException;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 采购合同 合同的签署结论 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2023-03-28
 */
@Service("purch_contract_change_sign_confirm_underlineService")
public class PurchSupplementContractSignConclusionServiceImpl extends ServiceImpl<PurchSupplementContractSignConclusionMapper,
        PurchSupplementContractSignConclusion> implements IPurchSupplementContractSignConclusionService {

//    @Autowired
//    private UserService userService;
//
//    @Autowired
//    private BusinessAssigneeService assigneeService;

    @Autowired
    private ICPurchContractSupplementElectronicInfoService purchContractSupplementElectronicInfoService;

    @Autowired
    private ICPurchContractManagementService purchContractService;

    @Autowired
    private ICPurchContractSupplementInfoService supplementInfoService;

    @Autowired
    private DictionaryService dictService;

    @Autowired
    private FileService fileStorageService;

    @Autowired
    private IPurchContractSupplementApprovalService purchContractSupplementApprovalService;

    @Autowired
    private ICPurchContractBasicInfoService purchContractBasicInfoService;

    @Autowired
    private IPurchSupplementContractSignConclusionService purchSupplementContractSignConclusionService;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${trade.purchContractChangeSignPageUrl}")
    private String purchContractChangeSignPageUrl;

    @Value("${trade.purchContractChangeDoSignPageUrl}")
    private String purchContractChangeDoSignPageUrl;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sign(PurchSupplementContractSignConclusion purchSupplementContractSignConclusion) throws IOException {

        try {
            restTemplate.postForObject(purchContractChangeDoSignPageUrl, purchSupplementContractSignConclusion, String.class);
        } catch (Exception e) {
            throw new BusinessException(CodeUtil.FAIL, "合同尚未签署完成，无法提交！");
        }
    }

    @Override
    public PurchSupplementContractSignConclusion getView(Long id) {
        PurchSupplementContractSignConclusion purchSupplementContractSignConclusion = getById(id);


        //只有一个文件
        List<FileStorageDO> files = fileStorageService.findByBusinessId(id, FileTypeEnum.PURCHASE_SUPPLEMENT_CONTRACT_SIGN_UNDERLINE.getCode());
        purchSupplementContractSignConclusion.setFileStorages(files);
        PurchContractSupplementApproval purchContractSupplementApproval = purchContractSupplementApprovalService
                .lambdaQuery().eq(PurchContractSupplementApproval::getPurchContractSupplementInfoId,
                        purchSupplementContractSignConclusion.getPurchContractSupplementInfoId())
                .orderByDesc(PurchContractSupplementApproval::getId).last("limit 1").one();
        if (purchContractSupplementApproval.getSignMode().equals(1)) {
            purchSupplementContractSignConclusion.setOnline(1);
        } else if (purchContractSupplementApproval.getSignMode().equals(0)) {
            purchSupplementContractSignConclusion.setOnline(0);
        }
        return purchSupplementContractSignConclusion;
    }

    @Override
    public Object getDetail(Long businessKey) {

        JSONObject json = restTemplate.getForObject(purchContractChangeSignPageUrl + "?businessKey=" + businessKey, JSONObject.class);

        Object data = json.get("data");
        return data;

    }

}
