/*
 * 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.policy.info.modular.mlpDraftAudit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.policy.info.modular.mlpDraftAudit.param.*;
import com.unis.policy.info.modular.mlpDraftAudit.vo.MlpDraftAuditVO;
import com.unis.policy.info.modular.mlpDraftAudit.vo.MlpDraftModifyVO;
import com.unis.policy.info.modular.mlpDraftAudit.vo.PlanningFinishPageVO;
import com.unis.policy.info.modular.mlpDraftAudit.vo.PlanningFinishVO;
import com.unis.policy.info.modular.mlpDraftAuditExpert.entity.MlpDraftAuditExpert;
import com.unis.policy.info.modular.mlpDraftAuditExpert.mapper.MlpDraftAuditExpertMapper;
import com.unis.policy.info.modular.mlpDraftAuditExpert.param.MlpDraftAuditExpertAddParam;
import com.unis.policy.info.modular.mlpDraftModify.entity.MlpDraftModify;
import com.unis.policy.info.modular.mlpDraftModify.mapper.MlpDraftModifyMapper;
import com.unis.policy.info.modular.mlpDraftModify.service.MlpDraftModifyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.policy.info.modular.mlpDraftAudit.entity.MlpDraftAudit;
import com.unis.policy.info.modular.mlpDraftAudit.mapper.MlpDraftAuditMapper;
import com.unis.policy.info.modular.mlpDraftAudit.service.MlpDraftAuditService;

import java.util.Date;
import java.util.List;

/**
 * 中长期规划管理-专家评审Service接口实现类
 *
 * @author hjc
 * @date 2024/04/23 21:01
 **/
@Service
public class MlpDraftAuditServiceImpl extends ServiceImpl<MlpDraftAuditMapper, MlpDraftAudit> implements MlpDraftAuditService {


    @Autowired
    private MlpDraftModifyMapper mlpDraftModifyMapper;

    @Autowired
    private MlpDraftAuditMapper mlpDraftAuditMapper;

    @Autowired
    private MlpDraftAuditExpertMapper mlpDraftAuditExpertMapper;
    @Autowired
    private MlpDraftModifyService mlpDraftModifyService;

    @Override
    public Page<MlpDraftAudit> page(MlpDraftAuditPageParam mlpDraftAuditPageParam) {
        QueryWrapper<MlpDraftAudit> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(mlpDraftAuditPageParam.getPlanName())) {
            queryWrapper.like("plan_name", mlpDraftAuditPageParam.getPlanName());
        }
        if (ObjectUtil.isAllNotEmpty(mlpDraftAuditPageParam.getSortField(), mlpDraftAuditPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(mlpDraftAuditPageParam.getSortOrder());
            queryWrapper.orderBy(true, mlpDraftAuditPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(mlpDraftAuditPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(MlpDraftAudit::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(MlpDraftAuditAddParam addParam) {
       /* MlpDraftAudit mlpDraftAudit = BeanUtil.toBean(mlpDraftAuditAddParam, MlpDraftAudit.class);
        this.save(mlpDraftAudit);*/

        //通过评审规划的id获取到名称
        //状态为0是保存  1为发布
        //保存数据
        MlpDraftAudit mlpDraftAudit = new MlpDraftAudit();
        BeanUtil.copyProperties(addParam, mlpDraftAudit);
        mlpDraftAudit.setCreateTime(new Date());
        if (addParam.getStatus() == 1) {
            mlpDraftAudit.setPublishTime(new Date());
        }
        mlpDraftAuditMapper.insert(mlpDraftAudit);
        //保存专家评审-专家表
        List<MlpDraftAuditExpertAddParam> mlpDraftAuditExpertList = addParam.getMlpDraftAuditExpert();
        if (CollectionUtil.isEmpty(mlpDraftAuditExpertList)) {
            throw new CommonException("请输入专家评审信息!");
        }
        for (MlpDraftAuditExpertAddParam mlpDraftAuditExpertAddParam : mlpDraftAuditExpertList) {
            MlpDraftAuditExpert mlpDraftAuditExpert = new MlpDraftAuditExpert();
            BeanUtil.copyProperties(mlpDraftAuditExpertAddParam, mlpDraftAuditExpert);
            mlpDraftAuditExpert.setDraftAuditId(mlpDraftAudit.getId());
            mlpDraftAuditExpert.setCreateTime(new Date());
            mlpDraftAuditExpertMapper.insert(mlpDraftAuditExpert);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(MlpDraftAuditEditParam editParam) {
     /*   MlpDraftAudit mlpDraftAudit = this.queryEntity(mlpDraftAuditEditParam.getId());
        BeanUtil.copyProperties(mlpDraftAuditEditParam, mlpDraftAudit);
        this.updateById(mlpDraftAudit);*/


        //状态为0是保存  1为发布  修改为0的数据
        MlpDraftAudit mlpDraftAudit = mlpDraftAuditMapper.selectById(editParam.getId());
        if (mlpDraftAudit.getStatus() == 1) {
            throw new CommonException("此数据已发布，无法修改数据!");
        }
        //保存修改数据
        BeanUtil.copyProperties(editParam, mlpDraftAudit);
        mlpDraftAudit.setUpdateTime(new Date());
        if (editParam.getStatus() == 1) {
            mlpDraftAudit.setPublishTime(new Date());
        }
        mlpDraftAuditMapper.updateById(mlpDraftAudit);

        //删除专家表的数据
        mlpDraftAuditExpertMapper.deleteByDraftAuditId(mlpDraftAudit.getId());

        //保存修改专家评审-专家表
        List<MlpDraftAuditExpertAddParam> mlpDraftAuditExpertList = editParam.getMlpDraftAuditExpert();
        if (CollectionUtil.isEmpty(mlpDraftAuditExpertList)) {
            throw new CommonException("请输入专家评审信息!");
        }
        for (MlpDraftAuditExpertAddParam mlpDraftAuditExpertAddParam : mlpDraftAuditExpertList) {
            MlpDraftAuditExpert draftAuditExpert = new MlpDraftAuditExpert();
            BeanUtil.copyProperties(mlpDraftAuditExpertAddParam, draftAuditExpert);
            draftAuditExpert.setDraftAuditId(mlpDraftAudit.getId());
            draftAuditExpert.setUpdateTime(new Date());
            try {
                mlpDraftAuditExpertMapper.insert(draftAuditExpert);
            } catch (Exception e) {

                throw new CommonException("数据新增失败");
            }
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<MlpDraftModifyIdParam> mlpDraftAuditIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(mlpDraftAuditIdParamList, MlpDraftModifyIdParam::getId));
        //删除专家评审数据
        LambdaQueryWrapper<MlpDraftAuditExpert> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MlpDraftAuditExpert::getDraftAuditId, mlpDraftAuditIdParamList.get(0).getId());
        List<MlpDraftAuditExpert> mlpDraftAuditExperts = mlpDraftAuditExpertMapper.selectList(wrapper);
        for (MlpDraftAuditExpert mlpDraftAuditExpert : mlpDraftAuditExperts) {
            mlpDraftAuditExpertMapper.deleteById(mlpDraftAuditExpert.getId());
        }

    }

    @Override
    public MlpDraftAuditVO detail(MlpDraftModifyIdParam mlpDraftAuditIdParam) {
        /*return this.queryEntity(mlpDraftAuditIdParam.getId());*/

        //通过传入的id查询数据
        MlpDraftAuditVO mlpDraftAuditVO = new MlpDraftAuditVO();
        MlpDraftAuditVO vo = mlpDraftAuditMapper.select(mlpDraftAuditIdParam.getId());
        List<MlpDraftAuditExpertAddParam> list = mlpDraftAuditExpertMapper.selectExpert(vo.getDraftAuditId());
        BeanUtil.copyProperties(vo, mlpDraftAuditVO);
        mlpDraftAuditVO.setMlpDraftAuditExpert(list);
        return mlpDraftAuditVO;

    }

    @Override
    public MlpDraftAudit queryEntity(String id) {
        MlpDraftAudit mlpDraftAudit = this.getById(id);
        if (ObjectUtil.isEmpty(mlpDraftAudit)) {
            throw new CommonException("中长期规划管理-专家评审不存在，id值为：{}", id);
        }
        return mlpDraftAudit;
    }

    /**
     * 获取中长期规划管理-规划id获取详情
     *
     * @author hjc
     * @date 2024/04/23 21:01
     */
    @Override
    public MlpDraftModify getDraftModify(MlpDraftModifyIdParam mlpDraftModifyIdParam) {

        return mlpDraftModifyMapper.selectById(mlpDraftModifyIdParam.getId());

    }

    /**
     * 获取中长期规划管理-规划完成，查看详情
     *
     * @author hjc
     * @date 2024/04/23 21:01
     */
    @Override
    public PlanningFinishVO getPlanningFinish(PlanningFinishIdParam planningFinishIdParam) {
        //查看规划id查看信息
        MlpDraftAudit mlpDraftAudit=this.getById(planningFinishIdParam.getId());
        if(mlpDraftAudit==null){
            throw new CommonException("未找到相关评审信息");
        }
        MlpDraftModify mlpDraftModify=mlpDraftModifyService.getById(mlpDraftAudit.getDraftModifyId());
        if(mlpDraftModify==null){
            throw new CommonException("未找到相关规划信息");
        }

        return new PlanningFinishVO(mlpDraftAudit,mlpDraftModify);
//        return mlpDraftAuditMapper.selectPlanningFinish(planningFinishIdParam.getId());


    }

    /**
     * 获取中长期规划管理-规划完成，分页
     *
     * @author hjc
     * @date 2024/04/23 21:01
     */
    @Override
    public Page<PlanningFinishPageVO> planningFinishPage(PlanningFinishPageParam pageParam) {

        Page<PlanningFinishPageVO> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        return mlpDraftAuditMapper.page(page, pageParam.getPlanName());

    }

    /**
     * 获取中长期规划管理-规划完成，发布
     *
     * @author hjc
     * @date 2024/04/23 21:01
     */
    @Override
    public void publish(PlanningFinishPubishParam planningFinishPubishParam) {

        //获取到评审id
        String draftAuditId = planningFinishPubishParam.getId();
        //通过评审id查询到专家评审信息
        MlpDraftAudit mlpDraftAudit = mlpDraftAuditMapper.selectByDraftModifyId(draftAuditId);
        //更新状态
        mlpDraftAudit.setStatus(planningFinishPubishParam.getStatus());
        mlpDraftAudit.setPublishTime(new Date());

        mlpDraftAuditMapper.updateById(mlpDraftAudit);

    }


    @Override
    public List<MlpDraftModifyVO> getDraftModifyAll() {


        return mlpDraftModifyMapper.select();

    }
}
