package com.ruoyi.worktecket.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.DeletedEnum;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.worktecket.domain.WorkTicketSmOption;
import com.ruoyi.worktecket.dto.WorkTicketSafetyMeasuresDTO;
import com.ruoyi.worktecket.dto.WorkTicketSmOptionDTO;
import com.ruoyi.worktecket.mapper.WorkTicketSmOptionMapper;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.worktecket.mapper.WorkTicketSafetyMeasuresMapper;
import com.ruoyi.worktecket.domain.WorkTicketSafetyMeasures;
import com.ruoyi.worktecket.service.IWorkTicketSafetyMeasuresService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 作业票安全措施模板Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-04
 */
@Service
public class WorkTicketSafetyMeasuresServiceImpl implements IWorkTicketSafetyMeasuresService {
    @Autowired
    private WorkTicketSafetyMeasuresMapper workTicketSafetyMeasuresMapper;

    @Autowired
    private WorkTicketSmOptionMapper workTicketSmOptionMapper;

    /**
     * 查询作业票安全措施模板
     *
     * @param id 作业票安全措施模板主键
     * @return 作业票安全措施模板
     */
    @Override
    public WorkTicketSafetyMeasuresDTO selectWorkTicketSafetyMeasuresById(String id) {
        WorkTicketSafetyMeasures workTicketSafetyMeasures = workTicketSafetyMeasuresMapper.selectById(id);
        if (Objects.isNull(workTicketSafetyMeasures)) {
            return null;
        }
        WorkTicketSafetyMeasuresDTO workTicketSafetyMeasuresDTO = new WorkTicketSafetyMeasuresDTO();
        BeanUtils.copyProperties(workTicketSafetyMeasures, workTicketSafetyMeasuresDTO);
        //查询安全措施列表
        LambdaQueryWrapper<WorkTicketSmOption> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkTicketSmOption::getIsDel, DeletedEnum.VALID.getValue());
        queryWrapper.eq(WorkTicketSmOption::getWorkTicketSmId, id);
        List<WorkTicketSmOption> workTicketSmOptions = workTicketSmOptionMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(workTicketSmOptions)) {
            ArrayList<WorkTicketSmOptionDTO> workTicketSmOptionDTOS = new ArrayList<>();
            for (WorkTicketSmOption workTicketSmOption : workTicketSmOptions) {
                WorkTicketSmOptionDTO workTicketSmOptionDTO = new WorkTicketSmOptionDTO();
                BeanUtils.copyProperties(workTicketSmOption, workTicketSmOptionDTO);
                workTicketSmOptionDTOS.add(workTicketSmOptionDTO);
            }
            workTicketSafetyMeasuresDTO.setWorkTicketSmOptionDTOList(workTicketSmOptionDTOS);
        }
        return workTicketSafetyMeasuresDTO;
    }

    /**
     * 查询作业票安全措施模板列表
     *
     * @param workTicketSafetyMeasuresDTO 作业票安全措施模板
     * @return 作业票安全措施模板
     */
    @Override
    public List<WorkTicketSafetyMeasuresDTO> selectWorkTicketSafetyMeasuresList(WorkTicketSafetyMeasuresDTO workTicketSafetyMeasuresDTO) {

//        String workTicketOptionName = workTicketSafetyMeasuresDTO.getWorkTicketOptionName();

        LambdaQueryWrapper<WorkTicketSafetyMeasures> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkTicketSafetyMeasures::getIsDel, DeletedEnum.VALID.getValue());
        queryWrapper.eq(StringUtils.isNotEmpty(workTicketSafetyMeasuresDTO.getWorkTicketLevel()), WorkTicketSafetyMeasures::getWorkTicketLevel, workTicketSafetyMeasuresDTO.getWorkTicketLevel());
        queryWrapper.eq(StringUtils.isNotEmpty(workTicketSafetyMeasuresDTO.getWorkTicketType()), WorkTicketSafetyMeasures::getWorkTicketType, workTicketSafetyMeasuresDTO.getWorkTicketType());
//        queryWrapper.like(!StringUtils.isEmpty(workTicketSafetyMeasuresDTO.getWorkTicketOptionName()),WorkTicketSafetyMeasures::getWorkTicketLevel,workTicketSafetyMeasuresDTO.getWorkTicketLevel());
        queryWrapper.like(StringUtils.isNotEmpty(workTicketSafetyMeasuresDTO.getWorkTicketOptionName()), WorkTicketSafetyMeasures::getWorkTicketOptionName, workTicketSafetyMeasuresDTO.getWorkTicketOptionName());
        queryWrapper.orderByAsc(WorkTicketSafetyMeasures::getCreateDate);
        List<WorkTicketSafetyMeasures> workTicketSafetyMeasuresList = workTicketSafetyMeasuresMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(workTicketSafetyMeasuresList)) {
            return Lists.newArrayList();
        }
        List<WorkTicketSafetyMeasuresDTO> list = new ArrayList<>();
        for (WorkTicketSafetyMeasures workTicketSafetyMeasures1 : workTicketSafetyMeasuresList) {
            WorkTicketSafetyMeasuresDTO workTicketSafetyMeasuresDTO1 = new WorkTicketSafetyMeasuresDTO();
            BeanUtils.copyProperties(workTicketSafetyMeasures1, workTicketSafetyMeasuresDTO1);
            list.add(workTicketSafetyMeasuresDTO1);
        }
        return list;
    }

    /**
     * 新增作业票安全措施模板
     *
     * @param workTicketSafetyMeasuresDTO 作业票安全措施模板
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWorkTicketSafetyMeasures(WorkTicketSafetyMeasuresDTO workTicketSafetyMeasuresDTO) {
        WorkTicketSafetyMeasures workTicketSafetyMeasures = new WorkTicketSafetyMeasures();
        BeanUtils.copyProperties(workTicketSafetyMeasuresDTO, workTicketSafetyMeasures);
        workTicketSafetyMeasures.setCreateDefault();
        int count = workTicketSafetyMeasuresMapper.insert(workTicketSafetyMeasures);
        List<WorkTicketSmOptionDTO> workTicketSmOptionDTOList = workTicketSafetyMeasuresDTO.getWorkTicketSmOptionDTOList();

        if (!CollectionUtils.isEmpty(workTicketSmOptionDTOList)) {
            for (WorkTicketSmOptionDTO workTicketSmOptionDTO : workTicketSmOptionDTOList) {
                WorkTicketSmOption workTicketSmOption = new WorkTicketSmOption();
                BeanUtils.copyProperties(workTicketSmOptionDTO, workTicketSmOption);
                workTicketSmOption.setWorkTicketSmId(workTicketSafetyMeasures.getId());
                workTicketSmOption.setCreateDefault();
                workTicketSmOptionMapper.insert(workTicketSmOption);
            }
        }
        return count;
    }

    /**
     * 修改作业票安全措施模板
     *
     * @param workTicketSafetyMeasuresDTO 作业票安全措施模板
     * @return 结果
     */
    @Override
    @Transactional
    public int updateWorkTicketSafetyMeasures(WorkTicketSafetyMeasuresDTO workTicketSafetyMeasuresDTO) {
        WorkTicketSafetyMeasures workTicketSafetyMeasures = new WorkTicketSafetyMeasures();
        BeanUtils.copyProperties(workTicketSafetyMeasuresDTO, workTicketSafetyMeasures);
        workTicketSafetyMeasures.setUpdateDefault();
        int count = workTicketSafetyMeasuresMapper.updateById(workTicketSafetyMeasures);

        List<WorkTicketSmOptionDTO> workTicketSmOptionDTOList = workTicketSafetyMeasuresDTO.getWorkTicketSmOptionDTOList();
        if (!CollectionUtils.isEmpty(workTicketSmOptionDTOList)) {
            //先删除后新增
            WorkTicketSmOption updateSmOption = new WorkTicketSmOption();
            updateSmOption.setUpdateDefault();
            updateSmOption.setIsDel(DeletedEnum.INVALID.getValue());
            QueryWrapper<WorkTicketSmOption> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("work_ticket_sm_id", workTicketSafetyMeasuresDTO.getId());
            workTicketSmOptionMapper.update(updateSmOption, queryWrapper);
            //新增
            for (WorkTicketSmOptionDTO workTicketSmOptionDTO : workTicketSmOptionDTOList) {
                WorkTicketSmOption workTicketSmOption = new WorkTicketSmOption();
                BeanUtils.copyProperties(workTicketSmOptionDTO, workTicketSmOption);
                workTicketSmOption.setWorkTicketSmId(workTicketSafetyMeasures.getId());
                workTicketSmOption.setCreateDefault();
                workTicketSmOption.setId(null);
                workTicketSmOptionMapper.insert(workTicketSmOption);
            }
        }
        return count;
    }

    /**
     * 批量删除作业票安全措施模板
     *
     * @param ids 需要删除的作业票安全措施模板主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteWorkTicketSafetyMeasuresByIds(String[] ids) {
        //先删除模板数据
        WorkTicketSafetyMeasures workTicketSafetyMeasures = new WorkTicketSafetyMeasures();
        workTicketSafetyMeasures.setUpdateDefault();
        workTicketSafetyMeasures.setIsDel(DeletedEnum.INVALID.getValue());
        QueryWrapper<WorkTicketSafetyMeasures> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        int count = workTicketSafetyMeasuresMapper.update(workTicketSafetyMeasures, queryWrapper);
        //后删除模板下措施项
        WorkTicketSmOption updateSmOption = new WorkTicketSmOption();
        updateSmOption.setUpdateDefault();
        updateSmOption.setIsDel(DeletedEnum.INVALID.getValue());
        QueryWrapper<WorkTicketSmOption> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.in("work_ticket_sm_id", ids);
        workTicketSmOptionMapper.update(updateSmOption, queryWrapper2);
        return count;
    }

    /**
     * 列表分页查询
     *
     * @param workTicketSafetyMeasuresDTO
     * @return
     */
    @Override
    public List<WorkTicketSafetyMeasures> selectList(WorkTicketSafetyMeasuresDTO workTicketSafetyMeasuresDTO) {
        LambdaQueryWrapper<WorkTicketSafetyMeasures> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkTicketSafetyMeasures::getIsDel, DeletedEnum.VALID.getValue());
        queryWrapper.eq(StringUtils.isNotEmpty(workTicketSafetyMeasuresDTO.getWorkTicketLevel()), WorkTicketSafetyMeasures::getWorkTicketLevel, workTicketSafetyMeasuresDTO.getWorkTicketLevel());
        queryWrapper.eq(StringUtils.isNotEmpty(workTicketSafetyMeasuresDTO.getWorkTicketType()), WorkTicketSafetyMeasures::getWorkTicketType, workTicketSafetyMeasuresDTO.getWorkTicketType());
//        queryWrapper.like(!StringUtils.isEmpty(workTicketSafetyMeasuresDTO.getWorkTicketOptionName()),WorkTicketSafetyMeasures::getWorkTicketLevel,workTicketSafetyMeasuresDTO.getWorkTicketLevel());
        queryWrapper.like(StringUtils.isNotEmpty(workTicketSafetyMeasuresDTO.getWorkTicketOptionName()), WorkTicketSafetyMeasures::getWorkTicketOptionName, workTicketSafetyMeasuresDTO.getWorkTicketOptionName());
        queryWrapper.orderByAsc(WorkTicketSafetyMeasures::getCreateDate);
        List<WorkTicketSafetyMeasures> workTicketSafetyMeasuresList = workTicketSafetyMeasuresMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(workTicketSafetyMeasuresList)) {
            return Lists.newArrayList();
        }
        return workTicketSafetyMeasuresList;
    }
}
