package com.dsk.system.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.dsk.common.core.domain.AjaxResult;
import com.dsk.common.core.domain.entity.BusinessInfo;
import com.dsk.common.core.domain.entity.BusinessLabel;
import com.dsk.common.core.domain.entity.BusinessRelateCompany;
import com.dsk.common.core.domain.entity.BusinessUser;
import com.dsk.common.dtos.BusinessInfoDto;
import com.dsk.common.utils.DateUtils;
import com.dsk.common.utils.SecurityUtils;
import com.dsk.system.domain.BusinessAddDto;
import com.dsk.system.domain.BusinessListDto;
import com.dsk.system.domain.customer.dto.CustomerBusinessSearchDto;
import com.dsk.system.domain.customer.vo.CustomerBusinessListVo;
import com.dsk.system.domain.vo.BusinessBrowseVo;
import com.dsk.system.domain.vo.BusinessListVo;
import com.dsk.system.mapper.BusinessInfoMapper;
import com.dsk.system.mapper.BusinessLabelMapper;
import com.dsk.system.mapper.BusinessRelateCompanyMapper;
import com.dsk.system.mapper.BusinessUserMapper;
import com.dsk.system.service.IBusinessInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 项目详情Service业务层处理
 *
 * @author lxl
 * @date 2023-05-17
 */
@Service
public class BusinessInfoServiceImpl implements IBusinessInfoService {
    @Resource
    private BusinessInfoMapper businessInfoMapper;
    @Resource
    private BusinessUserMapper businessUserMapper;
    @Resource
    private BusinessRelateCompanyMapper businessRelateCompanyMapper;
    @Resource
    private BusinessLabelMapper businessLabelMapper;

    /**
     * 查询项目详情
     *
     * @param id 项目详情主键
     * @return 项目详情
     */
    @Override
    public BusinessInfo selectBusinessInfoById(Integer id) {
        return businessInfoMapper.selectBusinessInfoById(id);
    }

    @Override
    public BusinessInfo getConstruction(Integer id) {
        return businessInfoMapper.getConstruction(id);
    }

    /**
     * 查询项目详情列表
     *
     * @param dto 项目详情
     * @return 项目详情
     */
    @Override
    public List<BusinessListVo> selectBusinessInfoList(BusinessListDto dto) {
        return businessInfoMapper.selectBusinessInfoList(dto);
    }

    @Override
    public BusinessBrowseVo browse(Integer businessId) {
        BusinessBrowseVo businessBrowseVo = new BusinessBrowseVo();
        //查询项目基本信息
        BusinessInfo businessInfo = businessInfoMapper.selectBusinessInfoById(businessId);
        BeanUtil.copyProperties(businessInfo,businessBrowseVo);
        //查询项目标签
        businessBrowseVo.setLabelList(businessLabelMapper.selectBusinessLabelList(new BusinessLabel(businessId)).stream().map(p -> p.getLabel()).collect(Collectors.toList()));
        //查询关键企业
        businessBrowseVo.setRelateCompany(businessRelateCompanyMapper.selectBusinessRelateCompanyList(new BusinessRelateCompany(businessId)));
        BusinessBrowseVo total = businessInfoMapper.selectTotal(businessId);
        businessBrowseVo.setBacklogCount(total.getBacklogCount());
        businessBrowseVo.setContactsCount(total.getContactsCount());
        businessBrowseVo.setFollowRecordCount(total.getFollowRecordCount());
        businessBrowseVo.setRelateCompanyCount(total.getRelateCompanyCount());
        return businessBrowseVo;
    }

    @Override
    public List<String> selectProjectName(BusinessListDto dto) {
        return businessInfoMapper.selectProjectName(dto);
    }

    /**
     * 新增项目详情
     *
     * @param dto 项目详情
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertBusinessInfo(BusinessAddDto dto) {
        //新增项目主信息
        BusinessInfo businessInfo = new BusinessInfo();
        BeanUtil.copyProperties(dto, businessInfo);
        int addBusiness = businessInfoMapper.insertBusinessInfo(businessInfo);
        if (addBusiness > 0) {
            //获取登陆用户的部门id
            Long deptId = SecurityUtils.getLoginUser().getDeptId();
//            Long deptId = 100l;
            //新增用户-项目关系信息
            int addbusinessUser = businessUserMapper.insertBusinessUser(new BusinessUser(businessInfo.getId(), deptId.intValue(), dto.getUserId(), 1));
            //新增项目-关联企业信息
            int addRelateCompany = businessRelateCompanyMapper.insertBusinessRelateCompany(new BusinessRelateCompany(businessInfo.getId(), dto.getCompanyId(), dto.getOwnerCompany(), "业主"));
            return addbusinessUser > 0 && addRelateCompany > 0 ? AjaxResult.success() : AjaxResult.error();
        }
        return AjaxResult.error();

    }

    /**
     * 修改项目详情
     *
     * @param businessInfo 项目详情
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBusinessInfo(BusinessInfo businessInfo)
    {
        businessInfo.setUpdateTime(DateUtils.getNowDate());
        return businessInfoMapper.updateBusinessInfo(businessInfo);
    }

    /**
     * 批量删除项目详情
     *
     * @param ids 需要删除的项目详情主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBusinessInfoByIds(Long[] ids) {
        return businessInfoMapper.deleteBusinessInfoByIds(ids);
    }

    /**
     * 删除项目详情信息
     *
     * @param id 项目详情主键
     * @return 结果
     */
    @Override
    public int deleteBusinessInfoById(Long id) {
        return businessInfoMapper.deleteBusinessInfoById(id);
    }

    /**
     * 查询项目数量
     *
     * @param status     项目状态
     * @param customerId 客户id
     * @return 项目数量
     */
    @Override
    public int selectCountByStatusAndCustomerId(Integer status, String customerId) {
        return businessInfoMapper.selectCountByStatusAndCustomerId(status, customerId);
    }

    @Override
    public List<CustomerBusinessListVo> selectCustomerBusinessList(CustomerBusinessSearchDto dto) {
        return businessInfoMapper.selectCustomerBusinessList(dto);
    }
}
