package com.yd.csf.api.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yd.common.enums.CommonEnum;
import com.yd.common.exception.BusinessException;
import com.yd.common.result.Result;
import com.yd.common.utils.CodeGenerator;
import com.yd.common.utils.RandomStringGenerator;
import com.yd.csf.api.service.ApiSalaryRemittanceService;
import com.yd.csf.api.service.ApiSalaryService;
import com.yd.csf.feign.request.salary.ApiSalaryAddRequest;
import com.yd.csf.feign.request.salary.ApiSalaryEditRequest;
import com.yd.csf.feign.request.salary.ApiSalaryPageRequest;
import com.yd.csf.feign.response.salary.ApiSalaryPageResponse;
import com.yd.csf.service.model.Salary;
import com.yd.csf.service.service.ISalaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ApiSalaryServiceImpl implements ApiSalaryService {

    @Autowired
    private ISalaryService iSalaryService;

    @Autowired
    private ApiSalaryRemittanceService apiSalaryRemittanceService;

    /**
     * 分页查询-薪资单信息
     * @param request
     * @return
     */
    @Override
    public Result<IPage<ApiSalaryPageResponse>> page(ApiSalaryPageRequest request) {
        return null;
    }

    /**
     * 新增-薪资单信息
     * @param request
     * @return
     */
    @Override
    public Result add(ApiSalaryAddRequest request) {
        //校验同一转介人同一薪资年月只能有一条薪资单记录
        checkUniqueByBrokerMonth(request.getBrokerBizId(),request.getMonth(),"",false);
        Salary salary = new Salary();
        BeanUtils.copyProperties(request,salary);
        //发放编号
        salary.setSalaryNo(CodeGenerator.generate10Code("SN"));
        //薪资单表唯一业务ID
        salary.setSalaryBizId(RandomStringGenerator.generateBizId16(CommonEnum.UID_TYPE_SALARY.getCode()));
        iSalaryService.saveOrUpdate(salary);

        //保存薪资汇款明细

        return Result.success();
    }

    /**
     * 修改-薪资单信息
     * @param request
     * @return
     */
    @Override
    public Result edit(ApiSalaryEditRequest request) {
        //校验薪资单信息是否存在
        Salary salary = checkIsExist(request.getSalaryBizId());
        //校验同一转介人同一薪资年月只能有一条薪资单记录
        checkUniqueByBrokerMonth(request.getBrokerBizId(),request.getMonth(),
                request.getSalaryBizId(),true);
        BeanUtils.copyProperties(request,salary);
        iSalaryService.saveOrUpdate(salary);

        //保存薪资汇款明细
        return null;
    }

    /**
     * 校验同一转介人同一年月只能有一条薪资单记录
     * @param brokerBizId 转介人业务id
     * @param month 薪资月份（同出账月，如：202605）
     * @param salaryBizId 薪资单信息唯一业务ID
     * @param isExcludeMy 是否排除自己
     */
    public void checkUniqueByBrokerMonth(String brokerBizId,String month,
                                         String salaryBizId,Boolean isExcludeMy) {
        Salary salary = iSalaryService.queryOne(brokerBizId,month,salaryBizId,isExcludeMy);
        if (salary != null) {
            throw new BusinessException("同一转介人同一薪资年月只能有一条薪资单记录");
        }
    }

    /**
     * 校验薪资单信息是否存在
     * @param salaryBizId
     */
    public Salary checkIsExist(String salaryBizId) {
        Salary salary = iSalaryService.queryOne(salaryBizId);
        if (salary == null) {
            throw new BusinessException("薪资单信息不存在");
        }
        return salary;
    }

}
