package com.hz.pm.api.fiscal.manage;

import com.hz.pm.api.common.util.StrUtils;
import com.hz.pm.api.fiscal.entity.CompanyFiscalCode;
import com.hz.pm.api.fiscal.model.po.CompanyFiscalCodeAndSealSnReq;
import com.hz.pm.api.fiscal.model.po.ReqGetCompanyFiscalCodeAndSealSnPO;
import com.hz.pm.api.fiscal.model.vo.CompanyFiscalCodeAndSealSnVO;
import com.hz.pm.api.fiscal.service.ICompanyFiscalCodeService;
import com.hz.pm.api.signature.entity.CompanySignature;
import com.hz.pm.api.signature.service.ICompanySignatureService;
import com.hz.pm.api.user.security.model.UserInfoDetails;
import com.hz.pm.api.user.util.LoginUserUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author liuxinxin
 * @date 2023/2/10 下午4:23
 */
@Component
@RequiredArgsConstructor
public class CompanyFiscalCodeManage {

    private final ICompanyFiscalCodeService fiscalCodeService;
    private final ICompanySignatureService signatureService;


    public void fiscalCodeConfigure(CompanyFiscalCodeAndSealSnReq req) {
        UserInfoDetails userDetail = LoginUserUtil.loginUserDetail();
        String temOrgCode;
        if (StrUtils.isBlank(req.getOrganizationCode())) {
            temOrgCode = String.valueOf(userDetail.getMhUnitId());
        } else {
            temOrgCode = req.getOrganizationCode();
        }
        // 保存财政编码配置 TODO 校验用户权限
        CompanyFiscalCode companyFiscalCode = fiscalCodeService.getByOrgCode(temOrgCode);
        if (Objects.isNull(companyFiscalCode)) {
            companyFiscalCode = new CompanyFiscalCode();
            companyFiscalCode.setOrganizationCode(temOrgCode);
            companyFiscalCode.setCreateOn(LocalDateTime.now());
            companyFiscalCode.setCreateBy(userDetail.getUserId());
        }
        String fiscalCode = req.getFiscalCode();
        companyFiscalCode.setFiscalCode(fiscalCode);
        companyFiscalCode.setUpdateOn(LocalDateTime.now());
        companyFiscalCode.setUpdateBy(userDetail.getUserId());
        fiscalCodeService.saveOrUpdate(companyFiscalCode);


        // 保存印章配置
        CompanySignature signature = signatureService.getByOrganizationCode(temOrgCode);
        if (Objects.isNull(signature)) {
            signature = new CompanySignature();
            signature.setOrganizationCode(temOrgCode);
            signature.setCreateOn(LocalDateTime.now());
        }
        String sealSn = req.getSealSn();
        signature.setSealSn(sealSn);
        signature.setUpdateOn(LocalDateTime.now());
        signature.setCreateBy(userDetail.getUserId());
        signature.setUpdateBy(userDetail.getUserId());
        signatureService.saveOrUpdate(signature);
    }

    public CompanyFiscalCodeAndSealSnVO getFiscalCodeConfigure(ReqGetCompanyFiscalCodeAndSealSnPO request) {
        UserInfoDetails userDetail = LoginUserUtil.loginUserDetail();
        String temOrgCode;
        if (StrUtils.isBlank(request.getOrganizationCode())) {
            temOrgCode = String.valueOf(userDetail.getMhUnitId());
        } else {
            temOrgCode = request.getOrganizationCode();
        }
        CompanySignature signature = signatureService.getByOrganizationCode(temOrgCode);
        if (Objects.isNull(signature)) {
            return null;
        }
        CompanyFiscalCode cfc = fiscalCodeService.getByOrgCode(temOrgCode);
        CompanyFiscalCodeAndSealSnVO res = new CompanyFiscalCodeAndSealSnVO();
        res.setCreateTime(signature.getCreateOn());
        res.setUpdateTime(signature.getUpdateOn());
        res.setFiscalCode(cfc.getFiscalCode());
        res.setSealSn(signature.getSealSn());
        return res;
    }
}
