package com.ruoyi.ehsFloodDroughtWorker.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.ehsFloodDroughtTeam.domain.EhsFloodDroughtTeam;
import com.ruoyi.ehsFloodDroughtTeam.mapper.EhsFloodDroughtTeamMapper;
import com.ruoyi.ehsFloodDroughtTeam.vo.FloodDroughtTeamVo;
import com.ruoyi.ehsFloodDroughtWorker.domain.FloodDroughtWorkerImportTemplate;
import com.ruoyi.ehsRescueTeamWorker.domain.EhsRescueTeamWorker;
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.ehsFloodDroughtWorker.mapper.EhsFloodDroughtWorkerMapper;
import com.ruoyi.ehsFloodDroughtWorker.domain.EhsFloodDroughtWorker;
import com.ruoyi.ehsFloodDroughtWorker.service.IEhsFloodDroughtWorkerService;

/**
 * 防汛抗旱队伍人员信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-14
 */
@Service
public class EhsFloodDroughtWorkerServiceImpl extends ServiceImpl<EhsFloodDroughtWorkerMapper, EhsFloodDroughtWorker> implements IEhsFloodDroughtWorkerService {

    @Autowired
    private EhsFloodDroughtWorkerMapper ehsFloodDroughtWorkerMapper;
    @Autowired
    private EhsFloodDroughtTeamMapper floodDroughtTeamMapper;

    /**
     * 查询防汛抗旱队伍人员信息
     *
     * @param id 防汛抗旱队伍人员信息主键
     * @return 防汛抗旱队伍人员信息
     */
    @Override
    public EhsFloodDroughtWorker selectEhsFloodDroughtWorkerById(Long id) {
        return ehsFloodDroughtWorkerMapper.selectEhsFloodDroughtWorkerById(id);
    }

    /**
     * 查询防汛抗旱队伍人员信息列表
     *
     * @param ehsFloodDroughtWorker 防汛抗旱队伍人员信息
     * @return 防汛抗旱队伍人员信息
     */
    @Override
    public List<EhsFloodDroughtWorker> selectEhsFloodDroughtWorkerList(EhsFloodDroughtWorker ehsFloodDroughtWorker) {
        return ehsFloodDroughtWorkerMapper.selectEhsFloodDroughtWorkerList(ehsFloodDroughtWorker);
    }

    /**
     * 根据防汛抗旱队伍ids删除队伍中的人员信息
     * @param teamIds 队伍ids
     * @return
     */
    @Override
    public boolean removeWorkerByTeamIds(List<Long> teamIds) {
        return ehsFloodDroughtWorkerMapper.removeWorkerByTeamIds(teamIds);
    }

    // 根据teamId更新防汛抗旱人员信息
    @Override
    public boolean updateWorkerByTeamId(EhsFloodDroughtTeam ehsFloodDroughtTeam) {
        EhsFloodDroughtWorker worker = new EhsFloodDroughtWorker();
        worker.setTeamId(ehsFloodDroughtTeam.getId());
        worker.setTeamName(ehsFloodDroughtTeam.getTeamName());
        return ehsFloodDroughtWorkerMapper.updateWorkerByTeamId(worker);
    }

    //批量导入 params是防汛抗旱队伍id
    @Override
    public String importData(List<FloodDroughtWorkerImportTemplate> 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 (FloodDroughtWorkerImportTemplate workerTemplate : list) {

            EhsFloodDroughtWorker temp = new EhsFloodDroughtWorker();

            // temp.setId(IdUtils.fastSimpleUUID());
            temp.setTeamId(params);// 队伍id
            FloodDroughtTeamVo floodDroughtTeamVo = floodDroughtTeamMapper.getTeamInfoByTeamId(params);
            temp.setTeamName(floodDroughtTeamVo.getTeamName());// 队伍名称
            temp.setWorkerName(workerTemplate.getWorkerName());// 人员姓名
            temp.setGender(workerTemplate.getGender());// 性别
            temp.setIdCard(workerTemplate.getIdCard());// 身份证号
            temp.setPhone(workerTemplate.getPhone());// 联系方式
            temp.setWorkYear(workerTemplate.getWorkYear());// 工作年限
            temp.setSpeciality(workerTemplate.getSpeciality());// 特长
            temp.setRemark(workerTemplate.getRemark());// 备注
            temp.setWorkerAge(workerTemplate.getWorkerAge());// 年龄
            temp.setTeamPost(workerTemplate.getTeamPost());// 队内职务
            temp.setTrainNum(workerTemplate.getTrainNum());// 培训次数
            try {
                //插入数据
                this.save(temp);
                successNum++;
                successMsg.append("<br />" + successNum + "、防汛抗旱队伍信息" + temp.getTeamName() + "导入成功");
            } catch (Exception e) {
                e.printStackTrace();
                log.error(e.getLocalizedMessage());
                failureNum++;
                successMsg.append("<br /><font color='red'>" + failureNum + "、防汛抗旱队伍信息" + temp.getTeamName() + "导入异常，请联系管理员！</font>");
                continue;
            }
        }
        return successMsg.toString();
    }


    private LambdaQueryWrapper<EhsFloodDroughtWorker> buildQueryWrapper(EhsFloodDroughtWorker query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<EhsFloodDroughtWorker> lqw = Wrappers.lambdaQuery();
        lqw.eq(query.getTeamId() != null, EhsFloodDroughtWorker::getTeamId, query.getTeamId());
        lqw.like(StringUtils.isNotBlank(query.getTeamName()), EhsFloodDroughtWorker::getTeamName, query.getTeamName());
        lqw.like(StringUtils.isNotBlank(query.getWorkerName()), EhsFloodDroughtWorker::getWorkerName, query.getWorkerName());
        lqw.eq(query.getGender() != null, EhsFloodDroughtWorker::getGender, query.getGender());
        lqw.eq(StringUtils.isNotBlank(query.getIdCard()), EhsFloodDroughtWorker::getIdCard, query.getIdCard());
        lqw.eq(StringUtils.isNotBlank(query.getPhone()), EhsFloodDroughtWorker::getPhone, query.getPhone());
        lqw.eq(StringUtils.isNotBlank(query.getWorkYear()), EhsFloodDroughtWorker::getWorkYear, query.getWorkYear());
        lqw.eq(StringUtils.isNotBlank(query.getSpeciality()), EhsFloodDroughtWorker::getSpeciality, query.getSpeciality());
        lqw.orderByDesc(EhsFloodDroughtWorker::getCreateTime);
        lqw.eq(query.getWorkerAge() != null, EhsFloodDroughtWorker::getWorkerAge, query.getWorkerAge());
        lqw.eq(StringUtils.isNotBlank(query.getTeamPost()), EhsFloodDroughtWorker::getTeamPost, query.getTeamPost());
        lqw.eq(query.getTrainNum() != null, EhsFloodDroughtWorker::getTrainNum, query.getTrainNum());
        lqw.eq(query.getDeptId() != null, EhsFloodDroughtWorker::getDeptId, query.getDeptId());
        return lqw;
    }

}
