package com.ruoyi.ehsFloodDroughtTeam.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.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ehsFloodDroughtTeam.domain.FloodDroughtTeamImportTemplate;
import com.ruoyi.ehsFloodDroughtTeam.vo.FloodDroughtTeamVo;
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.ehsFloodDroughtTeam.mapper.EhsFloodDroughtTeamMapper;
import com.ruoyi.ehsFloodDroughtTeam.domain.EhsFloodDroughtTeam;
import com.ruoyi.ehsFloodDroughtTeam.service.IEhsFloodDroughtTeamService;

/**
 * 防汛抗旱队伍管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-14
 */
@Service
public class EhsFloodDroughtTeamServiceImpl extends ServiceImpl<EhsFloodDroughtTeamMapper, EhsFloodDroughtTeam> implements IEhsFloodDroughtTeamService {

    @Autowired
    private EhsFloodDroughtTeamMapper ehsFloodDroughtTeamMapper;

    /**
     * 查询防汛抗旱队伍管理
     *
     * @param id 防汛抗旱队伍管理主键
     * @return 防汛抗旱队伍管理
     */
    @Override
    public EhsFloodDroughtTeam selectEhsFloodDroughtTeamById(Long id) {
        return ehsFloodDroughtTeamMapper.selectEhsFloodDroughtTeamById(id);
    }

    /**
     * 查询防汛抗旱队伍管理列表
     *
     * @param ehsFloodDroughtTeam 防汛抗旱队伍管理
     * @return 防汛抗旱队伍管理
     */
    @Override
    public List<EhsFloodDroughtTeam> selectEhsFloodDroughtTeamList(EhsFloodDroughtTeam ehsFloodDroughtTeam) {
        // 如果不是局端,那么只能查询自己部门下的数据
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        Long deptParentId = currentUser.getDept().getParentId();
        if (deptParentId != null && deptParentId.longValue() != 0) {
            ehsFloodDroughtTeam.setDeptId(currentUser.getDept().getDeptId().longValue());
        }
        List<EhsFloodDroughtTeam> teamList = ehsFloodDroughtTeamMapper.selectEhsFloodDroughtTeamList(ehsFloodDroughtTeam);
        // 给队伍设置人数信息
        teamList.stream().forEach(item -> {
            Long count = ehsFloodDroughtTeamMapper.getWorkerNumByTeamId(item.getId());
            if (count != null) {
                item.setRescueWorkerNum(count);
            }
        });
        return teamList;
    }

    /**
     * 防汛抗旱:根据队伍id获取队伍信息
     */
    @Override
    public FloodDroughtTeamVo getTeamInfoByTeamId(Long teamId) {
        return ehsFloodDroughtTeamMapper.getTeamInfoByTeamId(teamId);
    }

    @Override
    public String importData(List<FloodDroughtTeamImportTemplate> list, Boolean updateSupport) 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 (FloodDroughtTeamImportTemplate floodDroughtTeamTemplate : list) {

            EhsFloodDroughtTeam temp = new EhsFloodDroughtTeam();

            // temp.setId(IdUtils.fastSimpleUUID());
            temp.setTeamName(floodDroughtTeamTemplate.getTeamName());  //队伍名称
            temp.setTeamLevel(floodDroughtTeamTemplate.getTeamLevel()); //队伍级别
            temp.setDutyer(floodDroughtTeamTemplate.getDutyer());// 负责人
            temp.setPhone(floodDroughtTeamTemplate.getPhone());// 联系电话
            temp.setUnit(floodDroughtTeamTemplate.getUnit());// 所属单位
            temp.setStation(floodDroughtTeamTemplate.getStation());// 驻地
            temp.setTeamProperty(floodDroughtTeamTemplate.getTeamProperty());// 队伍属性
            temp.setRemark(floodDroughtTeamTemplate.getRemark()); // 备注

            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<EhsFloodDroughtTeam> buildQueryWrapper(EhsFloodDroughtTeam query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<EhsFloodDroughtTeam> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(query.getTeamName()), EhsFloodDroughtTeam::getTeamName, query.getTeamName());
        lqw.eq(StringUtils.isNotBlank(query.getTeamLevel()), EhsFloodDroughtTeam::getTeamLevel, query.getTeamLevel());
        lqw.orderByDesc(EhsFloodDroughtTeam::getCreateTime);
        lqw.eq(query.getDeptId() != null, EhsFloodDroughtTeam::getDeptId, query.getDeptId());
        lqw.like(StringUtils.isNotBlank(query.getDutyer()), EhsFloodDroughtTeam::getDutyer, query.getDutyer());
        lqw.eq(StringUtils.isNotBlank(query.getPhone()), EhsFloodDroughtTeam::getPhone, query.getPhone());
        lqw.eq(StringUtils.isNotBlank(query.getUnit()), EhsFloodDroughtTeam::getUnit, query.getUnit());
        lqw.like(StringUtils.isNotBlank(query.getStation()), EhsFloodDroughtTeam::getStation, query.getStation());
        return lqw;
    }

}
