package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dao.SysUserContractRecordDao;
import com.yonge.cooleshow.biz.dal.dto.search.SysUserContractRecordSearch;
import com.yonge.cooleshow.biz.dal.entity.ContractTemplate;
import com.yonge.cooleshow.biz.dal.entity.SysUserContractRecord;
import com.yonge.cooleshow.biz.dal.enums.DealStatusEnum;
import com.yonge.cooleshow.biz.dal.sdk.WithdrawSdk;
import com.yonge.cooleshow.biz.dal.service.ContractService;
import com.yonge.cooleshow.biz.dal.service.ContractTemplateService;
import com.yonge.cooleshow.biz.dal.service.SysUserContractRecordService;
import com.yonge.cooleshow.biz.dal.vo.SysUserContractRecordVo;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.enums.ContractTemplateSourceEnum;
import com.yonge.cooleshow.common.enums.ContractTemplateTypeEnum;
import com.yonge.cooleshow.common.enums.SysUserType;
import com.yonge.cooleshow.common.service.IdGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 用户协议记录表-一个用户一种协议一个版本一条记录(SysUserContractRecord)表服务实现类
 *
 * @author hgw
 * @since 2022-05-07 15:04:06
 */
@Service("sysUserContractRecordService")
public class SysUserContractRecordServiceImpl extends ServiceImpl<SysUserContractRecordDao, SysUserContractRecord> implements SysUserContractRecordService {

    private final static Logger log = LoggerFactory.getLogger(SysUserContractRecordServiceImpl.class);

    @Autowired
    private ContractTemplateService contractTemplateService;
    @Autowired
    private WithdrawSdk withdrawSdk;
    @Autowired
    private IdGeneratorService idGeneratorService;
    @Autowired
    private ContractService contractService;

    @Override
    public SysUserContractRecordDao getDao() {
        return this.baseMapper;
    }

    @Override
    public SysUserContractRecordVo detail(Long id) {
        return baseMapper.detail(id);
    }

    @Override
    public IPage<SysUserContractRecordVo> selectPage(IPage<SysUserContractRecordVo> page, SysUserContractRecordSearch query) {
        return page.setRecords(baseMapper.selectPage(page, query));
    }

    @Override
    public Boolean checkContractSign(Long userId, SysUserType userType, ContractTemplateTypeEnum contractType) {
        return getDao().checkContractSign(userId, userType.getCode(), contractType.getSource().getCode(), contractType.getCode());
    }

    @Override
    public SysUserContractRecord queryLatestContract(Long userId, SysUserType userType, ContractTemplateTypeEnum contractType) {
        return getDao().queryLatestContract(userId, userType.getCode(), contractType.getSource().getCode(), contractType.getCode());
    }

    @Override
    public HttpResponseResult<Boolean> sign(ContractTemplateTypeEnum contractType, SysUserType userType, SysUser user) {
        //是否签署标识，签署了才入库
        boolean flag = false;
        SysUserContractRecord contractRecord = queryLatestContract(user.getId(), userType, contractType);

        //未签署协议
        if (null == contractRecord) {
            ContractTemplate contractTemplate = contractTemplateService.queryLatestContractTemplate(contractType);
            if (null == contractTemplate) {
                return HttpResponseResult.failed("未找到协议");
            }
            contractRecord = new SysUserContractRecord();
            Long contractNo = idGeneratorService.generatorId("contractNo");
            contractRecord.setContractNo(contractNo.toString());
            contractRecord.setUserId(user.getId());
            contractRecord.setContractId(contractTemplate.getId());
            contractRecord.setUserType(userType.getCode());

            if (ContractTemplateSourceEnum.LINGXINPAY.getCode().equals(contractTemplate.getSource())
                    && ContractTemplateTypeEnum.WITHDRAW.getCode().equals(contractTemplate.getType())) {
                //签署结算协议
                contractRecord.setStatus(DealStatusEnum.DOING.getCode());
                flag = withdrawSdk.signContract(user.getRealName(), user.getIdCardNo(), user.getPhone(), contractNo.toString());
            } else {
                HttpResponseResult<String> stringHttpResponseResult = contractService.transferProduceContract(contractRecord.getUserId(), userType, contractType);
                if (stringHttpResponseResult.getStatus()) {
                    contractRecord.setUrl(stringHttpResponseResult.getData());
                    contractRecord.setStatus(DealStatusEnum.SUCCESS.getCode());
                    flag = true;
                }
            }
            if (flag) {
                save(contractRecord);
            }
        }
        return HttpResponseResult.succeed(true);
    }

}

