package com.dsk.biz.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dsk.biz.domain.BusinessRelateCompany;
import com.dsk.biz.domain.bo.BusinessIdDto;
import com.dsk.jsk.service.EnterpriseService;
import com.dsk.biz.mapper.BusinessRelateCompanyMapper;
import com.dsk.biz.service.IBusinessRelateCompanyService;
import com.dsk.common.core.domain.PageQuery;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.common.exception.base.BaseException;
import com.dsk.common.utils.CheckUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 项目关联单位Service业务层处理
 *
 * @author lxl
 * @date 2023-05-17
 */
@Service
public class BusinessRelateCompanyServiceImpl implements IBusinessRelateCompanyService {
    @Resource
    private BusinessRelateCompanyMapper businessRelateCompanyMapper;
    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 查询项目关联单位
     *
     * @param id 项目关联单位主键
     * @return 项目关联单位
     */
    @Override
    public BusinessRelateCompany selectBusinessRelateCompanyById(Long id) {
        return businessRelateCompanyMapper.selectBusinessRelateCompanyById(id);
    }

    /**
     * 查询项目关联单位列表
     *
     * @param bo 项目关联单位
     * @return 项目关联单位
     */
    @Override
    public TableDataInfo<BusinessRelateCompany> selectBusinessRelateCompanyList(BusinessRelateCompany bo, PageQuery pageQuery) {
        return TableDataInfo.build(businessRelateCompanyMapper.selectBusinessRelateCompanyList(pageQuery.build(), bo));
    }

    /**
     * 新增项目关联单位
     *
     * @param businessRelateCompany 项目关联单位
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBusinessRelateCompany(BusinessRelateCompany businessRelateCompany) {
        if (ObjectUtil.isNotEmpty(businessRelateCompany.getPhone()) && !CheckUtils.isPhone(businessRelateCompany.getPhone())) {
            throw new BaseException("500", "请输入正确的电话号码");
        }
        //查询企业的城投id和建设库id
        Map<String, Object> map = enterpriseService.getCidAndUipIdByCompanyName(businessRelateCompany.getCompanyName());
        businessRelateCompany.setCompanyUipId(MapUtils.getString(map, "uipId", null));
        businessRelateCompany.setCompanyId(MapUtils.getInteger(map, "companyId", null));
        return businessRelateCompanyMapper.insertBusinessRelateCompany(businessRelateCompany);
    }

    /**
     * 修改项目关联单位
     *
     * @param businessRelateCompany 项目关联单位
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBusinessRelateCompany(BusinessRelateCompany businessRelateCompany) {
        if (ObjectUtil.isNotEmpty(businessRelateCompany.getPhone()) && !CheckUtils.isPhone(businessRelateCompany.getPhone())) {
            throw new BaseException("500", "请输入正确的电话号码");
        }
        //查询企业的城投id和建设库id
        Map<String, Object> map = enterpriseService.getCidAndUipIdByCompanyName(businessRelateCompany.getCompanyName());
        businessRelateCompany.setCompanyUipId(MapUtils.getString(map, "uipId", null));
        businessRelateCompany.setCompanyId(MapUtils.getInteger(map, "companyId", null));
        return businessRelateCompanyMapper.updateBusinessRelateCompany(businessRelateCompany);
    }

    /**
     * 批量删除项目关联单位
     *
     * @param ids 需要删除的项目关联单位主键
     * @return 结果
     */
    @Override
    public int deleteBusinessRelateCompanyByIds(Long[] ids) {
        return businessRelateCompanyMapper.deleteBusinessRelateCompanyByIds(ids);
    }

    /**
     * 删除项目关联单位信息
     *
     * @param id 项目关联单位主键
     * @return 结果
     */
    @Override
    public int deleteBusinessRelateCompanyById(Long id) {
        return businessRelateCompanyMapper.deleteBusinessRelateCompanyById(id);
    }

    @Override
    public List<String> companyRoleList(BusinessIdDto dto) {
        List<BusinessRelateCompany> roleList = businessRelateCompanyMapper.selectList(Wrappers.<BusinessRelateCompany>lambdaQuery()
                .select(BusinessRelateCompany::getCompanyRole)
                .eq(BusinessRelateCompany::getBusinessId, dto.getBusinessId())
                .groupBy(BusinessRelateCompany::getCompanyRole)
                .orderByDesc(BusinessRelateCompany::getCreateTime));
        return roleList.stream().map(p -> p.getCompanyRole()).collect(Collectors.toList());
    }
}
