package com.trade.service.enterprise.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trade.common.constant.HttpStatus;
import com.trade.common.exception.ServiceException;
import com.trade.common.utils.DateUtils;
import com.trade.service.DictConstants;
import com.trade.service.enterprise.customer.domain.EnterpriseChangeRecord;
import com.trade.service.enterprise.customer.dto.EnterpriseBaseInfoDto;
import com.trade.service.enterprise.customer.dto.EnterpriseDictVO;
import com.trade.service.enterprise.customer.dto.EnterpriseSearchDto;
import com.trade.service.enterprise.customer.dto.EnterpriseXtIdsDto;
import com.trade.service.enterprise.customer.mapper.EnterpriseMapper;
import com.trade.service.enterprise.customer.domain.Enterprise;
import com.trade.service.enterprise.customer.service.EnterpriseBaseService;
import com.trade.service.enterprise.customer.service.EnterpriseChangeRecordBaseService;
import com.trade.service.thirdparty.xiaotu.dto.UpdateEnterpriseRequest;
import com.trade.service.thirdparty.xiaotu.service.XiaotuRequestService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 企业信息基础服务实现类
 *
 * @author hlb
 * @since 2024-05-29 15:07:42
 */
@Service("enterpriseBaseService")
public class EnterpriseBaseServiceImpl extends ServiceImpl<EnterpriseMapper, Enterprise> implements EnterpriseBaseService {

    @Resource
    private EnterpriseMapper enterpriseMapper;
    @Resource
    private XiaotuRequestService xiaotuRequestService;
    @Resource
    private EnterpriseChangeRecordBaseService enterpriseChangeRecordBaseService;

    @Override
    public Enterprise getEnterpriseByCustomerId(Long customerId) {
        return this.lambdaQuery().eq(Enterprise::getCustomerId, customerId).orderByDesc(Enterprise::getGmtCreate)
                .last("LIMIT 1").one();
    }

    @Override
    public Enterprise getEnterpriseByChangeRecordId(Long changeRecordId) {
        return this.lambdaQuery().eq(Enterprise::getChangeRecordId, changeRecordId)
                .orderByDesc(Enterprise::getGmtCreate).last("LIMIT 1").one();
    }

    @Override
    public Enterprise getEnterpriseByCertNo(String certNo) {
        return lambdaQuery().eq(Enterprise::getCertNo, certNo).one();
    }

    @Override
    public List<EnterpriseBaseInfoDto> findBySearch(EnterpriseSearchDto search) {
        return enterpriseMapper.findBySearch(search);
    }

    @Override
    public List<EnterpriseBaseInfoDto> findByCustomerInfo(String customerInfo) {
        return enterpriseMapper.findByCustomerInfo(customerInfo);
    }

    @Override
    public List<EnterpriseDictVO> queryAllEnterprise4Dict() {
        return enterpriseMapper.queryAllEnterprise4Dict();
    }

    @Override
    public EnterpriseXtIdsDto getEnterpriseXtIdsDto(String certNo) {
        return enterpriseMapper.getEnterpriseXtIdsDto(certNo);
    }

    @Override
    public Boolean taxRebateServiceFeePayment(Long customerUserId, String xtUid, Date paymentDate) {
        Enterprise enterprise = this.getEnterpriseByCustomerId(customerUserId);
        if (enterprise == null) {
            throw new ServiceException("未找到企业信息", HttpStatus.BAD_REQUEST);
        }
        UpdateEnterpriseRequest updateEnterpriseRequest = new UpdateEnterpriseRequest();
        updateEnterpriseRequest.setSbqyuuid(enterprise.getXtEid());
        updateEnterpriseRequest.setHgqyDm(enterprise.getCustomsEnterpriseCode());
        updateEnterpriseRequest.setNsrmc(enterprise.getName());
        updateEnterpriseRequest.setNsrsbh(enterprise.getTaxNo());
        updateEnterpriseRequest.setDqcode(enterprise.getTaxAuthorityArea());
        updateEnterpriseRequest.setCktsqylxDm(enterprise.getEnterpriseType());
        updateEnterpriseRequest.setYhuuid1(xtUid);
        updateEnterpriseRequest.setHstqysqbz(DictConstants.SYS_YES);
        updateEnterpriseRequest.setTsfwxybz(DictConstants.SYS_YES);
        xiaotuRequestService.updateEnterprise(updateEnterpriseRequest);
        // 更新更新退税服务协议有效起讫时间
        Date startDate = DateUtils.atStartOfDay(paymentDate);
        Date endDate = DateUtils.atEndOfDay(DateUtils.plusYear(startDate, 1));
        this.lambdaUpdate().eq(Enterprise::getId, enterprise.getId())
                .set(Enterprise::getTaxRebateEffectiveStart, startDate)
                .set(Enterprise::getTaxRebateEffectiveEnd, endDate).update();
        enterpriseChangeRecordBaseService.lambdaUpdate()
                .eq(EnterpriseChangeRecord::getId, enterprise.getChangeRecordId())
                .set(EnterpriseChangeRecord::getTaxRebateEffectiveStart, startDate)
                .set(EnterpriseChangeRecord::getTaxRebateEffectiveEnd, endDate).update();
        return true;
    }
}
