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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.entity.contract.dto.ContractTemplateDto;
import com.fjhx.common.entity.contract.dto.ContractTemplateSelectDto;
import com.fjhx.common.entity.contract.po.ContractTemplate;
import com.fjhx.common.entity.contract.vo.ContractTemplateVo;
import com.fjhx.common.mapper.contract.ContractTemplateMapper;
import com.fjhx.common.service.contract.ContractTemplateService;
import com.fjhx.tenant.utils.DeptUstil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.common.utils.wrapper.SqlField;
import org.springframework.stereotype.Service;

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


/**
 * <p>
 * 合同模板 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-14
 */
@Service
public class ContractTemplateServiceImpl extends ServiceImpl<ContractTemplateMapper, ContractTemplate> implements ContractTemplateService {

    @Override
    public Page<ContractTemplateVo> getPage(ContractTemplateSelectDto dto) {
        IWrapper<ContractTemplate> wrapper = getWrapper();
        wrapper.keyword(dto, new SqlField("ct", ContractTemplate::getTemplateName));

        //合同类型过滤
        wrapper.eq("ct", ContractTemplate::getTemplateType, dto.getTemplateType());

        //权限过滤：合同模板-子公司看自己的,总公司看全部
        Long companyId = SecurityUtils.getCompanyId();
        if (!Objects.equals(companyId, 100L)) {
            wrapper.eq("ct.company_id", companyId);
        } else {
            wrapper.eq("ct.company_id", dto.getCompanyId());
        }

        wrapper.orderByDesc("ct", ContractTemplate::getId);
        Page<ContractTemplateVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);

        List<ContractTemplateVo> records = page.getRecords();

        if (records.size() == 0) {
            return page;
        }

        //获取生产公司信息
        List<Long> companyIds = records.stream().map(ContractTemplateVo::getCompanyId).collect(Collectors.toList());
        Map<Long, String> companyNameMap = DeptUstil.getDeptNameMap(companyIds);

        for (ContractTemplateVo record : records) {
            //赋值生产公司名称
            record.setCompanyName(companyNameMap.get(record.getCompanyId()));
        }

        return page;

    }

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

    @Override
    public void add(ContractTemplateDto contractTemplateDto) {

        //赋值归属公司id
        contractTemplateDto.setCompanyId(SecurityUtils.getCompanyId());

        this.save(contractTemplateDto);
    }

    @Override
    public void edit(ContractTemplateDto contractTemplateDto) {
        this.updateById(contractTemplateDto);
    }

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

}
