package com.cool.store.service.impl;

import com.cool.store.dao.ContractConfigDAO;
import com.cool.store.dto.contract.ContractConfigDTO;
import com.cool.store.dto.contract.ContractListDTO;
import com.cool.store.dto.contract.QueryContractListDTO;
import com.cool.store.entity.ContractConfigDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.service.ContractConfigService;
import com.cool.store.utils.poi.constant.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author suzhuhong
 * @Date 2025/9/8 15:47
 * @Version 1.0
 */
@Service
public class ContractConfigServiceImpl implements ContractConfigService {

    @Resource
    private ContractConfigDAO contractConfigDAO;

    @Override
    public Boolean addContractConfig(ContractConfigDTO addContractConfigDTO) {
        ContractConfigDO contractConfig = contractConfigDAO.queryContractConfigByBrand(addContractConfigDTO.getBrand(), addContractConfigDTO.getSerialNumber());
        if (contractConfig != null){
            throw new ServiceException(ErrorCodeEnum.CURRENT_BRAND_SORT_NUMBER_EXIST);
        }
        ContractConfigDO contractConfigDO = new ContractConfigDO();
        BeanUtils.copyProperties(addContractConfigDTO, contractConfigDO);
        if (addContractConfigDTO.getFranchiseModeList() != null){
            String modelStr = addContractConfigDTO.getFranchiseModeList().stream().collect(Collectors.joining(","));
            contractConfigDO.setFranchiseMode("," + modelStr + ",");
        }
        contractConfigDAO.addContractConfig(contractConfigDO);
        return Boolean.TRUE;
    }

    @Override
    public Boolean updateContractConfig(ContractConfigDTO addContractConfigDTO) {
        ContractConfigDO contractConfig = contractConfigDAO.queryContractConfigByBrand(addContractConfigDTO.getBrand(), addContractConfigDTO.getSerialNumber());
        if (contractConfig != null && !contractConfig.getId().equals(addContractConfigDTO.getId())){
            throw new ServiceException(ErrorCodeEnum.CURRENT_BRAND_SORT_NUMBER_EXIST);
        }
        ContractConfigDO contractConfigDO = new ContractConfigDO();
        BeanUtils.copyProperties(addContractConfigDTO, contractConfigDO);
        if (addContractConfigDTO.getFranchiseModeList() != null){
            String modelStr = addContractConfigDTO.getFranchiseModeList().stream().collect(Collectors.joining(","));
            contractConfigDO.setFranchiseMode("," + modelStr + ",");
        }
        contractConfigDAO.updateContractConfig(contractConfigDO);
        return Boolean.TRUE;
    }

    @Override
    public PageInfo<ContractListDTO> queryContractConfigList(QueryContractListDTO queryContractListDTO) {
        PageHelper.startPage(queryContractListDTO.getPageNum(), queryContractListDTO.getPageSize());
        List<ContractConfigDO> contractConfigDOList = contractConfigDAO.queryContractConfigList(queryContractListDTO.getBrand());
        if (CollectionUtils.isEmpty(contractConfigDOList)){
            return new PageInfo<>();
        }
        PageInfo contractConfigDOPageInfo = new PageInfo<>(contractConfigDOList);
        List<ContractListDTO> result = new ArrayList<>();
        contractConfigDOList.forEach(contractConfigDO -> {
            ContractListDTO contractListDTO = new ContractListDTO();
            BeanUtils.copyProperties(contractConfigDO, contractListDTO);
            result.add(contractListDTO);
        });
        contractConfigDOPageInfo.setList( result);
        return contractConfigDOPageInfo;
    }

    @Override
    public Boolean deleteContractConfig(Long id) {
        ContractConfigDO contractConfigDO = contractConfigDAO.queryContractConfigById(id);
        if (contractConfigDO == null){
            throw new ServiceException(ErrorCodeEnum.CONTRACT_CONFIG_NOT_EXIST);
        }
        contractConfigDO.setDeleted(Constants.ONE_INTEGER);
        //修改配置状态
        contractConfigDAO.updateContractConfig(contractConfigDO);
        return Boolean.TRUE;
    }


}
