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.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.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @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 = userInfoHelper.getOrganizationCode(userId);
        // TODO 校验用户权限

        // 保存财政编码配置
        String fiscalCode = reqCompanyFiscalCodeAndSealSnPO.getFiscalCode();
        CompanyFiscalCode companyFiscalCodeSaveRecord = new CompanyFiscalCode();
        companyFiscalCodeSaveRecord.setFiscalCode(fiscalCode);
        companyFiscalCodeSaveRecord.setOrganizationCode(organizationCode);
        companyFiscalCodeSaveRecord.setCreateOn(LocalDateTime.now());
        companyFiscalCodeSaveRecord.setUpdateOn(LocalDateTime.now());
        companyFiscalCodeSaveRecord.setCreateBy(userId);
        companyFiscalCodeSaveRecord.setUpdateBy(userId);
        iCompanyFiscalCodeService.save(companyFiscalCodeSaveRecord);

        // 保存印章配置
        String sealSn = reqCompanyFiscalCodeAndSealSnPO.getSealSn();
        CompanySignature companySignatureSaveRecord = new CompanySignature();
        companySignatureSaveRecord.setSealSn(sealSn);
        companySignatureSaveRecord.setOrganizationCode(organizationCode);
        companySignatureSaveRecord.setCreateOn(LocalDateTime.now());
        companySignatureSaveRecord.setUpdateOn(LocalDateTime.now());
        companySignatureSaveRecord.setCreateBy(userId);
        companySignatureSaveRecord.setUpdateBy(userId);
        iCompanySignatureService.save(companySignatureSaveRecord);
    }
}
