package com.ningdatech.pmapi.fiscal.manage;

import com.ningdatech.pmapi.common.helper.UserInfoHelper;
import com.ningdatech.pmapi.fiscal.entity.CompanyFiscalCode;
import com.ningdatech.pmapi.fiscal.model.po.ReqCompanyFiscalCodeAndSealSnPO;
import com.ningdatech.pmapi.fiscal.model.po.ReqGetCompanyFiscalCodeAndSealSnPO;
import com.ningdatech.pmapi.fiscal.model.vo.ResCompanyFiscalCodeAndSealSnVO;
import com.ningdatech.pmapi.fiscal.service.ICompanyFiscalCodeService;
import com.ningdatech.pmapi.signature.entity.CompanySignature;
import com.ningdatech.pmapi.signature.service.ICompanySignatureService;
import com.ningdatech.pmapi.user.util.LoginUserUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
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 UserInfoHelper userInfoHelper;

    private final ICompanyFiscalCodeService iCompanyFiscalCodeService;
    private final ICompanySignatureService iCompanySignatureService;


    public void fiscalCodeConfigure(ReqCompanyFiscalCodeAndSealSnPO reqCompanyFiscalCodeAndSealSnPO) {
        Long userId = LoginUserUtil.getUserId();

        String organizationCode = reqCompanyFiscalCodeAndSealSnPO.getOrganizationCode();
        if (StringUtils.isBlank(organizationCode)) {
            organizationCode = userInfoHelper.getUserEmpPosUnitCode(userId);
        }
        // TODO 校验用户权限

        // 保存财政编码配置
        CompanyFiscalCode companyFiscalCode = iCompanyFiscalCodeService.getByOrganizationCode(organizationCode);
        if (Objects.isNull(companyFiscalCode)) {
            companyFiscalCode = new CompanyFiscalCode();
            companyFiscalCode.setOrganizationCode(organizationCode);
            companyFiscalCode.setCreateOn(LocalDateTime.now());
            companyFiscalCode.setCreateBy(userId);
        }
        String fiscalCode = reqCompanyFiscalCodeAndSealSnPO.getFiscalCode();
        companyFiscalCode.setFiscalCode(fiscalCode);
        companyFiscalCode.setUpdateOn(LocalDateTime.now());
        companyFiscalCode.setUpdateBy(userId);
        iCompanyFiscalCodeService.saveOrUpdate(companyFiscalCode);


        // 保存印章配置
        CompanySignature companySignature = iCompanySignatureService.getByOrganizationCode(organizationCode);
        if (Objects.isNull(companySignature)) {
            companySignature = new CompanySignature();
            companySignature.setOrganizationCode(organizationCode);
            companySignature.setCreateOn(LocalDateTime.now());
        }
        String sealSn = reqCompanyFiscalCodeAndSealSnPO.getSealSn();
        companySignature.setSealSn(sealSn);
        companySignature.setUpdateOn(LocalDateTime.now());
        companySignature.setCreateBy(userId);
        companySignature.setUpdateBy(userId);
        iCompanySignatureService.saveOrUpdate(companySignature);
    }

    public ResCompanyFiscalCodeAndSealSnVO getFiscalCodeConfigure(ReqGetCompanyFiscalCodeAndSealSnPO request) {
        Long userId = LoginUserUtil.getUserId();
        String organizationCode = request.getOrganizationCode();
        if (StringUtils.isBlank(organizationCode)) {
            organizationCode = userInfoHelper.getUserEmpPosUnitCode(userId);
        }
        CompanySignature companySignature = iCompanySignatureService.getByOrganizationCode(organizationCode);
        CompanyFiscalCode companyFiscalCode = iCompanyFiscalCodeService.getByOrganizationCode(organizationCode);

        ResCompanyFiscalCodeAndSealSnVO returnVO = new ResCompanyFiscalCodeAndSealSnVO();
        if (Objects.isNull(companySignature)) {
            return null;
        }
        returnVO.setCreateTime(companySignature.getCreateOn());
        returnVO.setUpdateTime(companySignature.getUpdateOn());
        returnVO.setFiscalCode(companyFiscalCode.getFiscalCode());
        returnVO.setSealSn(companySignature.getSealSn());
        return returnVO;
    }
}
