package com.dsk.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dsk.common.core.domain.entity.BusinessRelateCompany;
import com.dsk.common.utils.DateUtils;
import com.dsk.system.domain.BusinessIdDto;
import com.dsk.system.mapper.BusinessRelateCompanyMapper;
import com.dsk.system.service.IBusinessRelateCompanyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

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

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

    /**
     * 查询项目关联单位列表
     *
     * @param businessRelateCompany 项目关联单位
     * @return 项目关联单位
     */
    @Override
    public List<BusinessRelateCompany> selectBusinessRelateCompanyList(BusinessRelateCompany businessRelateCompany)
    {
        return businessRelateCompanyMapper.selectBusinessRelateCompanyList(businessRelateCompany);
    }

    /**
     * 新增项目关联单位
     *
     * @param businessRelateCompany 项目关联单位
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBusinessRelateCompany(BusinessRelateCompany businessRelateCompany)
    {
//        if(!CheckUtils.isPhone(businessRelateCompany.getPhone())) throw new BaseException("500","请输入正确的电话号码");
        businessRelateCompany.setCreateTime(DateUtils.getNowDate());
        return businessRelateCompanyMapper.insertBusinessRelateCompany(businessRelateCompany);
    }

    /**
     * 修改项目关联单位
     *
     * @param businessRelateCompany 项目关联单位
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBusinessRelateCompany(BusinessRelateCompany businessRelateCompany)
    {
//        if(!CheckUtils.isPhone(businessRelateCompany.getPhone())) throw new BaseException("500","请输入正确的电话号码");
        businessRelateCompany.setUpdateTime(DateUtils.getNowDate());
        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());
    }
}
