package com.fjhx.sale.service.contract.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.enums.FlowStatusEnum1;
import com.fjhx.customer.service.customer.CustomerService;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.sale.entity.contract.dto.ContractDocumentaryDto;
import com.fjhx.sale.entity.contract.dto.ContractDocumentarySelectDto;
import com.fjhx.sale.entity.contract.dto.ContractSelectDto;
import com.fjhx.sale.entity.contract.enums.DocumentaryTypeEnum;
import com.fjhx.sale.entity.contract.po.Contract;
import com.fjhx.sale.entity.contract.po.ContractDocumentary;
import com.fjhx.sale.entity.contract.vo.ContractDocumentaryVo;
import com.fjhx.sale.mapper.contract.ContractDocumentaryMapper;
import com.fjhx.sale.service.contract.ContractDocumentaryService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 外销跟单表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-08-30
 */
@Service
public class ContractDocumentaryServiceImpl extends ServiceImpl<ContractDocumentaryMapper, ContractDocumentary> implements ContractDocumentaryService {

    @Autowired
    private CustomerService customerService;

    /**
     * 分页
     * @param dto
     * @return
     */
    @Override
    public Page<ContractDocumentary> getPage(ContractDocumentarySelectDto dto) {
        IWrapper<ContractDocumentary> wrapper = getWrapper();
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getBusinessId()),"business_id",dto.getBusinessId());
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getDocumentaryType()),"documentary_type",dto.getDocumentaryType());
        wrapper.orderByDesc("cd", ContractDocumentary::getId);
        Page<ContractDocumentary> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<ContractDocumentary> list = page.getRecords();
        // 赋值用户名称
        UserUtil.assignmentNickName(list, ContractDocumentary::getCreateUser, ContractDocumentary::setUserName);
        return page;
    }

    @Override
    public ContractDocumentaryVo detail(Long id) {
        ContractDocumentary ContractDocumentary = this.getById(id);
        ContractDocumentaryVo result = BeanUtil.toBean(ContractDocumentary, ContractDocumentaryVo.class);
        return result;
    }

    /**
     * 添加
     * @param contractDocumentaryDto
     */
    @Override
    @DSTransactional
    public void add(ContractDocumentaryDto contractDocumentaryDto) {
        this.save(contractDocumentaryDto);
        ObsFileUtil.saveFile(contractDocumentaryDto.getFileList(),contractDocumentaryDto.getId());
    }

    @Override
    public void edit(ContractDocumentaryDto contractDocumentaryDto) {
        this.updateById(contractDocumentaryDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

    /**
     * 统计数量
     * @return
     */
    @Override
    public Map<String,Integer> getSumCount(ContractSelectDto dto) {
        List<Long> authIdList = customerService.getAuthIdList();
        if (authIdList.size() == 0) {
            return new HashMap<>();
        }
        IWrapper<Contract> wrapper = IWrapper.getWrapper();
        wrapper.in("t1", Contract::getBuyCorporationId, authIdList);
        wrapper.orderByDesc("t1", Contract::getCreateTime);
        wrapper.eq("t1", Contract::getIsChange, "0");//列表只展示未变更得数据
        wrapper.ne("t1", Contract::getIsShow, 1);
        wrapper.eq("t1", Contract::getStatus, FlowStatusEnum1.PASS.getKey());
        wrapper.eq("t1.orderStatus", dto.getOrderStatus());
        if (dto.getDocumentarySearch() == 1 && ObjectUtil.isNotEmpty(dto.getDocumentaryType())) {//无记录
            wrapper.eq("t1.documentaryCount", 0);
        }
        if (dto.getDocumentarySearch() == 2 && ObjectUtil.isNotEmpty(dto.getDocumentaryType())) {//无记录
            wrapper.gt("t1.documentaryCount", 0);
        }
        wrapper.groupBy("t3.documentary_type", "t1.id");

        //合同id过滤
        wrapper.eq("t1", Contract::getId, dto.getId());

        if (StringUtils.isNotEmpty(dto.getKeyword())) {
            wrapper.and(q -> q
                    .like("t1", Contract::getCode, dto.getKeyword())
                    .or().like("t1.salesmanName", dto.getKeyword())
            );
        }

        List<ContractDocumentary> list = baseMapper.getSumCount(wrapper, dto.getDocumentaryType());
        Map<String, Integer> countMap = setCount(list);
        return countMap;
    }


    private Map<String,Integer> setCount(List<ContractDocumentary> list){
        if(CollectionUtils.isNotEmpty(list)){
            Map<String,Integer> map = list.stream().collect(Collectors.toMap(ContractDocumentary::getDocumentaryType,ContractDocumentary::getCount));
            map.put(DocumentaryTypeEnum.TYPE_1.getKey(),map.getOrDefault(DocumentaryTypeEnum.TYPE_1.getKey(),0));
            map.put(DocumentaryTypeEnum.TYPE_2.getKey(),map.getOrDefault(DocumentaryTypeEnum.TYPE_2.getKey(),0));
            map.put(DocumentaryTypeEnum.TYPE_3.getKey(),map.getOrDefault(DocumentaryTypeEnum.TYPE_3.getKey(),0));
            map.put(DocumentaryTypeEnum.TYPE_4.getKey(),map.getOrDefault(DocumentaryTypeEnum.TYPE_4.getKey(),0));
            map.put(DocumentaryTypeEnum.TYPE_5.getKey(),map.getOrDefault(DocumentaryTypeEnum.TYPE_5.getKey(),0));
            map.put(DocumentaryTypeEnum.TYPE_6.getKey(),map.getOrDefault(DocumentaryTypeEnum.TYPE_6.getKey(),0));
            map.put(DocumentaryTypeEnum.TYPE_7.getKey(),map.getOrDefault(DocumentaryTypeEnum.TYPE_7.getKey(),0));
            map.put(DocumentaryTypeEnum.TYPE_8.getKey(),map.getOrDefault(DocumentaryTypeEnum.TYPE_8.getKey(),0));
            map.put(DocumentaryTypeEnum.TYPE_9.getKey(),map.getOrDefault(DocumentaryTypeEnum.TYPE_9.getKey(),0));
            map.put(DocumentaryTypeEnum.TYPE_10.getKey(),map.getOrDefault(DocumentaryTypeEnum.TYPE_10.getKey(),0));
            map.put(DocumentaryTypeEnum.TYPE_11.getKey(),map.getOrDefault(DocumentaryTypeEnum.TYPE_11.getKey(),0));
            map.put(DocumentaryTypeEnum.TYPE_12.getKey(),map.getOrDefault(DocumentaryTypeEnum.TYPE_12.getKey(),0));
            return map;
        }
       return new HashMap<>();
    }

}
