/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.unis.emergencySupportCenter.modular.esEdProcessDesign.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.auth.core.util.StpClientUtil;
import com.unis.auth.modular.login.entity.UserCollectLogin;
import com.unis.basic.info.modular.grainPriceCollectCitySum.entity.GrainPriceCollectCitySum;
import com.unis.emergencySupportCenter.modular.esEdProcessDesign.entity.VO.EsEdProcessDesignSelectVO;
import com.unis.emergencySupportCenter.modular.esEdProcessDesign.param.EsEdProcessDesignSelectParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.unis.common.enums.CommonSortOrderEnum;
import com.unis.common.exception.CommonException;
import com.unis.common.page.CommonPageRequest;
import com.unis.emergencySupportCenter.modular.esEdProcessDesign.entity.EsEdProcessDesign;
import com.unis.emergencySupportCenter.modular.esEdProcessDesign.mapper.EsEdProcessDesignMapper;
import com.unis.emergencySupportCenter.modular.esEdProcessDesign.param.EsEdProcessDesignAddParam;
import com.unis.emergencySupportCenter.modular.esEdProcessDesign.param.EsEdProcessDesignEditParam;
import com.unis.emergencySupportCenter.modular.esEdProcessDesign.param.EsEdProcessDesignIdParam;
import com.unis.emergencySupportCenter.modular.esEdProcessDesign.param.EsEdProcessDesignPageParam;
import com.unis.emergencySupportCenter.modular.esEdProcessDesign.service.EsEdProcessDesignService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 应急演练-流程设计Service接口实现类
 *
 * @author TS
 * @date  2024/07/01 10:02
 **/
@Service
public class EsEdProcessDesignServiceImpl extends ServiceImpl<EsEdProcessDesignMapper, EsEdProcessDesign> implements EsEdProcessDesignService {
    @Resource
    private EsEdProcessDesignMapper esEdProcessDesignMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(EsEdProcessDesignAddParam esEdProcessDesignAddParam) {
        EsEdProcessDesign esEdProcessDesign = BeanUtil.toBean(esEdProcessDesignAddParam, EsEdProcessDesign.class);
        this.save(esEdProcessDesign);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(EsEdProcessDesignEditParam esEdProcessDesignEditParam) {
        EsEdProcessDesign esEdProcessDesign = this.queryEntity(esEdProcessDesignEditParam.getId());
        BeanUtil.copyProperties(esEdProcessDesignEditParam, esEdProcessDesign);
        this.updateById(esEdProcessDesign);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<EsEdProcessDesignIdParam> esEdProcessDesignIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(esEdProcessDesignIdParamList, EsEdProcessDesignIdParam::getId));
    }

    @Override
    public EsEdProcessDesign detail(EsEdProcessDesignIdParam esEdProcessDesignIdParam) {
        return this.queryEntity(esEdProcessDesignIdParam.getId());
    }

    @Override
    public EsEdProcessDesign queryEntity(String id) {
        EsEdProcessDesign esEdProcessDesign = this.getById(id);
        if(ObjectUtil.isEmpty(esEdProcessDesign)) {
            throw new CommonException("应急演练-流程设计不存在，id值为：{}", id);
        }
        return esEdProcessDesign;
    }

    /**
     * 获取应急演练-流程设计自定义分页
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<EsEdProcessDesign> findPage(EsEdProcessDesignPageParam pageParam) {
            //条形查询器
            QueryWrapper<EsEdProcessDesign> queryWrapper = new QueryWrapper<>();

            //分页相关
            if(StringUtils.isNotBlank(pageParam.getDrillTheme())){
                queryWrapper.like("drill_theme",pageParam.getDrillTheme() );
            }
            if(StringUtils.isNotBlank(pageParam.getResponsibleDepartment())){
                queryWrapper.like("responsible_department",pageParam.getResponsibleDepartment() );
            }
            Page<EsEdProcessDesign> p =new Page<>(pageParam.getCurrent(),pageParam.getSize());


            IPage<EsEdProcessDesign> page = esEdProcessDesignMapper.selectMyMapsPage(p,queryWrapper);
            return page;
    }

    /**
     * 获取应急演练-流程设计下拉列表
     *
     * @param edProcessDesignSelectParam
     * @return
     */
    @Override
    public List<EsEdProcessDesignSelectVO> getEsEdProcessDesignSelect(EsEdProcessDesignSelectParam edProcessDesignSelectParam) {
        return esEdProcessDesignMapper.getEsEdProcessDesignSelect(edProcessDesignSelectParam.getType());
    }
}
