package com.ruoyi.ehsRescueTeamWorker.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.ehsRescueTeam.domain.EhsRescueTeam;
import com.ruoyi.ehsRescueTeam.domain.RescueTeamImportTemplate;
import com.ruoyi.ehsRescueTeam.mapper.EhsRescueTeamMapper;
import com.ruoyi.ehsRescueTeam.vo.TeamResponseVo;
import com.ruoyi.ehsRescueTeamWorker.domain.RescueTeamWorkerImportTemplate;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.common.utils.StringUtils;

import java.util.List;
import java.util.Map;

import com.ruoyi.ehsRescueTeamWorker.mapper.EhsRescueTeamWorkerMapper;
import com.ruoyi.ehsRescueTeamWorker.domain.EhsRescueTeamWorker;
import com.ruoyi.ehsRescueTeamWorker.service.IEhsRescueTeamWorkerService;

/**
 * 应急救援队人员信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-11
 */
@Service
public class EhsRescueTeamWorkerServiceImpl extends ServiceImpl<EhsRescueTeamWorkerMapper, EhsRescueTeamWorker> implements IEhsRescueTeamWorkerService {

    @Autowired
    private EhsRescueTeamWorkerMapper ehsRescueTeamWorkerMapper;
    @Autowired
    private EhsRescueTeamMapper ehsRescueTeamMapper;

    /**
     * 查询应急救援队人员信息
     *
     * @param id 应急救援队人员信息主键
     * @return 应急救援队人员信息
     */
    @Override
    public EhsRescueTeamWorker selectEhsRescueTeamWorkerById(Long id) {
        return ehsRescueTeamWorkerMapper.selectEhsRescueTeamWorkerById(id);
    }

    /**
     * 查询应急救援队人员信息列表
     *
     * @param ehsRescueTeamWorker 应急救援队人员信息
     * @return 应急救援队人员信息
     */
    @Override
    public List<EhsRescueTeamWorker> selectEhsRescueTeamWorkerList(EhsRescueTeamWorker ehsRescueTeamWorker) {
        return ehsRescueTeamWorkerMapper.selectEhsRescueTeamWorkerList(ehsRescueTeamWorker);
    }

    // 根据队伍ids删除队伍中的队员
    @Override
    public boolean removeWorkerByTeamIds(List<Long> teamIds) {
        return ehsRescueTeamWorkerMapper.removeWorkerByTeamIds(teamIds);
    }

    /**
     * 导入模板数据
     * @param list
     * @param updateSupport
     * @Param params 参数
     * @return
     */
    @Override
    public String importRescueTeamWorker(List<EhsRescueTeamWorker> list, Boolean updateSupport, Long params) throws Exception {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new Exception("导入应急救援队伍人员信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;

        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (EhsRescueTeamWorker item : list) {

            EhsRescueTeamWorker temp = new EhsRescueTeamWorker();
            /**
             * 因为是插入数据,所以id需要处理,如果id是String类型,那么生成个uuid插入到数据库
             * 如果id是整数,并且自动递增,那么无需设置id
             */
            // temp.setId(IdUtils.fastSimpleUUID());
            // 根据teamId查询队伍信息,parms前端传过来的是teamId
            TeamResponseVo teamResponseVo = ehsRescueTeamMapper.getTeamInfoByTeamId(params);
            temp.setRescueTeamId(teamResponseVo.getRescueTeamId());// 队伍id
            temp.setRescueTeamName(teamResponseVo.getRescueTeamName());// 队伍名称
            temp.setWorkerName(item.getWorkerName());// 人员姓名
            temp.setGender(item.getGender());// 性别(0男 1女)
            temp.setIdCard(item.getIdCard());// 身份证号
            temp.setPhone(item.getPhone());// 联系方式
            temp.setWorkYear(item.getWorkYear());// 工作年限
            temp.setSpeciality(item.getSpeciality());// 救援技能特长
            temp.setRemark(item.getRemark());// 备注
            temp.setWorkerAge(item.getWorkerAge());// 年龄
            temp.setTeamPost(item.getTeamPost());// 队内职务
            temp.setTrainNum(item.getTrainNum());// 培训次数

            try {
                //插入数据
                this.save(temp);
                successNum++;
                successMsg.append("<br />" + successNum + "、应急救援队伍人员信息" + temp.getWorkerName() + "导入成功");
            } catch (Exception e) {
                e.printStackTrace();
                log.error(e.getLocalizedMessage());
                failureNum++;
                successMsg.append("<br /><font color='red'>" + failureNum + "、应急救援队伍人员信息" + temp.getWorkerName() + "导入异常，请联系管理员！</font>");
                continue;
            }
        }
        return successMsg.toString();
    }

    // 根据teamId更新应急救援人员信息
    @Override
    public boolean updateWorkerByTeamId(EhsRescueTeam ehsRescueTeam) {
        EhsRescueTeamWorker worker = new EhsRescueTeamWorker();
        worker.setRescueTeamId(ehsRescueTeam.getId());
        worker.setRescueTeamName(ehsRescueTeam.getTeamName());
        return ehsRescueTeamWorkerMapper.updateWorkerByTeamId(worker);
    }

    /**
     * 根据队伍id获取人员信息(分页)
     */
    @Override
    public List<EhsRescueTeamWorker> listWorker(Long rescueTeamId) {
        return ehsRescueTeamWorkerMapper.listWorker(rescueTeamId);
    }

    /**
     * 根据多个teamIds获取人员信息
     * @param teamIds 队伍id
     * @return
     */
    @Override
    public List<EhsRescueTeamWorker> listWorkerByTeamIds(List<Long> teamIds) {
        return ehsRescueTeamWorkerMapper.listWorkerByTeamIds(teamIds);
    }


    private LambdaQueryWrapper<EhsRescueTeamWorker> buildQueryWrapper(EhsRescueTeamWorker query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<EhsRescueTeamWorker> lqw = Wrappers.lambdaQuery();
        lqw.eq(query.getRescueTeamId() != null, EhsRescueTeamWorker::getRescueTeamId, query.getRescueTeamId());
        lqw.like(StringUtils.isNotBlank(query.getRescueTeamName()), EhsRescueTeamWorker::getRescueTeamName, query.getRescueTeamName());
        lqw.like(StringUtils.isNotBlank(query.getWorkerName()), EhsRescueTeamWorker::getWorkerName, query.getWorkerName());
        lqw.eq(query.getGender() != null, EhsRescueTeamWorker::getGender, query.getGender());
        lqw.eq(StringUtils.isNotBlank(query.getIdCard()), EhsRescueTeamWorker::getIdCard, query.getIdCard());
        lqw.eq(StringUtils.isNotBlank(query.getPhone()), EhsRescueTeamWorker::getPhone, query.getPhone());
        lqw.eq(StringUtils.isNotBlank(query.getWorkYear()), EhsRescueTeamWorker::getWorkYear, query.getWorkYear());
        lqw.eq(StringUtils.isNotBlank(query.getSpeciality()), EhsRescueTeamWorker::getSpeciality, query.getSpeciality());

        lqw.eq(query.getWorkerAge() != null, EhsRescueTeamWorker::getWorkerAge, query.getWorkerAge());
        lqw.eq(StringUtils.isNotBlank(query.getTeamPost()), EhsRescueTeamWorker::getTeamPost, query.getTeamPost());
        lqw.eq(query.getTrainNum() != null, EhsRescueTeamWorker::getTrainNum, query.getTrainNum());
        lqw.orderByDesc(EhsRescueTeamWorker::getCreateTime);
        return lqw;
    }

}
