package com.ruoyi.powerdistribution.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.powerdistribution.domain.PdmConstructor;
import com.ruoyi.powerdistribution.domain.excel.ConstructTeam;
import com.ruoyi.powerdistribution.domain.vo.PdmConstructTeamVo;
import com.ruoyi.powerdistribution.mapper.PdmConstructorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.powerdistribution.mapper.PdmConstructTeamMapper;
import com.ruoyi.powerdistribution.domain.PdmConstructTeam;
import com.ruoyi.powerdistribution.service.IPdmConstructTeamService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 配网工程队伍施工小组信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-20
 */
@Service
public class PdmConstructTeamServiceImpl implements IPdmConstructTeamService 
{
    @Autowired
    private PdmConstructTeamMapper pdmConstructTeamMapper;

    @Autowired
    private PdmConstructorMapper pdmConstructorMapper;

    /**
     * 查询配网工程队伍施工小组信息
     * 
     * @param id 配网工程队伍施工小组信息主键
     * @return 配网工程队伍施工小组信息
     */
    @Override
    public PdmConstructTeam selectPdmConstructTeamById(Long id)
    {
        return pdmConstructTeamMapper.selectPdmConstructTeamById(id);
    }

    /**
     * 查询配网工程队伍施工小组信息列表
     * 
     * @param pdmConstructTeam 配网工程队伍施工小组信息
     * @return 配网工程队伍施工小组信息
     */
    @Override
    public List<PdmConstructTeam> selectPdmConstructTeamList(PdmConstructTeam pdmConstructTeam)
    {
        return pdmConstructTeamMapper.selectPdmConstructTeamList(pdmConstructTeam);
    }

    /**
     * 新增配网工程队伍施工小组信息
     * 
     * @param pdmConstructTeam 配网工程队伍施工小组信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPdmConstructTeam(PdmConstructTeamVo pdmConstructTeam)
    {
        String teamNo= IdUtils.fastSimpleUUID();
        pdmConstructTeam.setCreateTime(DateUtils.getNowDate());
        pdmConstructTeam.setCreateBy(SecurityUtils.getUsername());
        pdmConstructTeam.setTeamNo(teamNo);
        pdmConstructTeam.getConstructorList().forEach(constructor->{
            constructor.setTeamNo(teamNo);
            pdmConstructorMapper.insertPdmConstructor(constructor);
        });
        return pdmConstructTeamMapper.insertPdmConstructTeam(pdmConstructTeam);
    }

    /**
     * 修改配网工程队伍施工小组信息
     * 
     * @param pdmConstructTeam 配网工程队伍施工小组信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePdmConstructTeam(PdmConstructTeamVo pdmConstructTeam)
    {
        pdmConstructTeam.setUpdateTime(DateUtils.getNowDate());
        pdmConstructTeam.setUpdateBy(SecurityUtils.getUsername());
        pdmConstructTeam.getConstructorList().forEach(constructor->{
           if(pdmConstructorMapper.updatePdmConstructor(constructor)<1){
               constructor.setTeamNo(pdmConstructTeam.getTeamNo());
               pdmConstructorMapper.insertPdmConstructor(constructor);
           };
        });
        return pdmConstructTeamMapper.updatePdmConstructTeam(pdmConstructTeam);
    }

    /**
     * 批量删除配网工程队伍施工小组信息
     * 
     * @param ids 需要删除的配网工程队伍施工小组信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletePdmConstructTeamByIds(Long[] ids)
    {
        pdmConstructorMapper.deletePdmConstructorByTeamIds(ids);
        return pdmConstructTeamMapper.deletePdmConstructTeamByIds(ids);
    }

    /**
     * 删除配网工程队伍施工小组信息信息
     * 
     * @param id 配网工程队伍施工小组信息主键
     * @return 结果
     */
    @Override
    public int deletePdmConstructTeamById(Long id)
    {
        pdmConstructorMapper.deletePdmConstructorByTeamId(id);
        return pdmConstructTeamMapper.deletePdmConstructTeamById(id);
    }

    @Override
    public void importExcel(List<ConstructTeam> constructTeamList) {
        Map<String,PdmConstructTeamVo> data=new HashMap<>();
        for (ConstructTeam constructTeam : constructTeamList) {
            String key=constructTeam.getTeamNm()+","+constructTeam.getLegalPerson()+","+constructTeam.getLegalContactPhone()
                    +","+constructTeam.getTeamLeader()+","+constructTeam.getTeamLeaderPhone();
            if(data.containsKey(key)){
                PdmConstructor constructor=new PdmConstructor();
                BeanUtils.copyProperties(constructTeam,constructor);
                PdmConstructTeamVo constructTeamVo=data.get(key);
                constructTeamVo.getConstructorList().add(constructor);
            }else{
                PdmConstructTeamVo constructTeamVo=new PdmConstructTeamVo();
                BeanUtils.copyProperties(constructTeam,constructTeamVo);
                constructTeamVo.setConstructorList(new ArrayList<>());
                data.put(key,constructTeamVo);
            }
        }
        data.values().forEach(dmConstructTeamVo->{
            insertPdmConstructTeam(dmConstructTeamVo);
        });
    }
}
