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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.area.utils.CustomizeAreaUtil;
import com.fjhx.common.entity.corporation.dto.CorporationDto;
import com.fjhx.common.entity.corporation.dto.CorporationSelectDto;
import com.fjhx.common.entity.corporation.po.Corporation;
import com.fjhx.common.entity.corporation.vo.CorporationVo;
import com.fjhx.common.mapper.corporation.CorporationMapper;
import com.fjhx.common.service.corporation.CorporationService;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.tenant.utils.DeptUstil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
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-10
 */
//@DS(SourceConstant.COMMON)
@Service
public class CorporationServiceImpl extends ServiceImpl<CorporationMapper, Corporation> implements CorporationService {

    @Override
    public Page<CorporationVo> getPage(CorporationSelectDto dto) {
        IWrapper<Corporation> wrapper = getWrapper();
        if (ObjectUtil.isNotEmpty(dto.getKeyword())) {
            wrapper.and(q -> q.like(Corporation::getName, dto.getKeyword())
                    .or().like(Corporation::getLegalPersonName, dto.getKeyword())
                    .or().like(Corporation::getNameEn, dto.getKeyword()));
        }

        //权限过滤：企业信息配置-子公司看自己的,总公司看全部
        Long companyId = SecurityUtils.getCompanyId();
        if (!Objects.equals(companyId, 100L)) {
            wrapper.eq("c.company_id", companyId);
        } else {
            wrapper.eq("c.company_id", dto.getCompanyId());
        }

        wrapper.orderByDesc("c", Corporation::getId);


        Page<CorporationVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<CorporationVo> records = page.getRecords();


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

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

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

        // 赋值国省市
        CustomizeAreaUtil.setAreaName(records);
        return page;
    }

    @Override
    public CorporationVo detail(Long id) {
        Corporation Corporation = this.getById(id);
        CorporationVo result = BeanUtil.toBean(Corporation, CorporationVo.class);
        // 赋值国省市
        CustomizeAreaUtil.setAreaName(result);
        return result;
    }

    @DSTransactional
    @Override
    public void add(CorporationDto corporationDto) {

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

        CustomizeAreaUtil.setAreaId(corporationDto);

        this.save(corporationDto);
        // 企业logo
        ObsFileUtil.saveFile(corporationDto.getEnterpriseLogoList(), corporationDto.getId(), 1);
        // 法人签名
        ObsFileUtil.saveFile(corporationDto.getLarSignList(), corporationDto.getId(), 2);
        // 公章
        ObsFileUtil.saveFile(corporationDto.getOfficialSealList(), corporationDto.getId(), 3);
        // 合同章
        ObsFileUtil.saveFile(corporationDto.getContractSealList(), corporationDto.getId(), 4);
    }

    @DSTransactional
    @Override
    public void edit(CorporationDto corporationDto) {

        CustomizeAreaUtil.setAreaId(corporationDto);

        this.updateById(corporationDto);
        // 企业logo
        ObsFileUtil.editFile(corporationDto.getEnterpriseLogoList(), corporationDto.getId(), 1);
        // 法人签名
        ObsFileUtil.editFile(corporationDto.getLarSignList(), corporationDto.getId(), 2);
        // 公章
        ObsFileUtil.editFile(corporationDto.getOfficialSealList(), corporationDto.getId(), 3);
        // 合同章
        ObsFileUtil.editFile(corporationDto.getContractSealList(), corporationDto.getId(), 4);
    }

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

}
