/*
 * 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.mlpFirstDraftCollectFeedback.service.impl;

import cn.dev33.satoken.stp.StpUtil;
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.alibaba.fastjson2.JSON;
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.policy.info.modular.mlpFirstDraft.mapper.MlpFirstDraftMapper;
import com.unis.policy.info.modular.mlpFirstDraftCollect.entity.MlpFirstDraftCollect;
import com.unis.policy.info.modular.mlpFirstDraftCollectFeedback.param.*;
import com.unis.sys.modular.user.entity.SysUser;
import com.unis.sys.modular.user.service.SysUserService;
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.policy.info.modular.mlpFirstDraftCollectFeedback.entity.MlpFirstDraftCollectFeedback;
import com.unis.policy.info.modular.mlpFirstDraftCollectFeedback.mapper.MlpFirstDraftCollectFeedbackMapper;
import com.unis.policy.info.modular.mlpFirstDraftCollectFeedback.service.MlpFirstDraftCollectFeedbackService;

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

/**
 * 中长期规划管理-初稿意见反馈Service接口实现类
 *
 * @author QSHan
 * @date 2024/04/18 20:06
 **/
@Service
public class MlpFirstDraftCollectFeedbackServiceImpl extends ServiceImpl<MlpFirstDraftCollectFeedbackMapper, MlpFirstDraftCollectFeedback> implements MlpFirstDraftCollectFeedbackService {
    @Resource
    private MlpFirstDraftMapper mlpFirstDraftMapper;
    @Resource
    private MlpFirstDraftCollectFeedbackMapper mlpFirstDraftCollectFeedbackMapper;
    @Resource
    private SysUserService sysUserService;

    @Override
    public Page<MlpFirstDraftCollectFeedback> page(MlpFirstDraftCollectFeedbackPageParam mlpFirstDraftCollectFeedbackPageParam, MlpFirstDraftCollectFeedbackQueryParam param) {
        QueryWrapper<MlpFirstDraftCollectFeedback> queryWrapper = new QueryWrapper<>();
        // 规划名称模糊查询
        if (StringUtils.isNotBlank(param.getPlanName())) {
            // 规划初稿,规划名称模糊查询,询所有匹配关联id
            List<String> mlpFirstDrafts = mlpFirstDraftMapper.resultIdList(param.getPlanName());
            if (!mlpFirstDrafts.isEmpty()) {
                queryWrapper.in("first_draft_collect_id", mlpFirstDrafts);
            }
        }

        if (ObjectUtil.isAllNotEmpty(mlpFirstDraftCollectFeedbackPageParam.getSortField(), mlpFirstDraftCollectFeedbackPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(mlpFirstDraftCollectFeedbackPageParam.getSortOrder());
            queryWrapper.orderBy(true, mlpFirstDraftCollectFeedbackPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(mlpFirstDraftCollectFeedbackPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(MlpFirstDraftCollectFeedback::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(MlpFirstDraftCollectFeedbackAddParam mlpFirstDraftCollectFeedbackAddParam) {
        MlpFirstDraftCollectFeedback mlpFirstDraftCollectFeedback = BeanUtil.toBean(mlpFirstDraftCollectFeedbackAddParam, MlpFirstDraftCollectFeedback.class);
        this.save(mlpFirstDraftCollectFeedback);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(MlpFirstDraftCollectFeedbackEditParam mlpFirstDraftCollectFeedbackEditParam) {
        MlpFirstDraftCollectFeedback mlpFirstDraftCollectFeedback = this.queryEntity(mlpFirstDraftCollectFeedbackEditParam.getId());
        BeanUtil.copyProperties(mlpFirstDraftCollectFeedbackEditParam, mlpFirstDraftCollectFeedback);
        this.updateById(mlpFirstDraftCollectFeedback);
    }

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

    @Override
    public MlpFirstDraftCollectFeedback detail(MlpFirstDraftCollectFeedbackQueryParam param) {
        QueryWrapper<MlpFirstDraftCollectFeedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("first_draft_collect_id", param.getId());
        return mlpFirstDraftCollectFeedbackMapper.selectOne(queryWrapper);
//        return this.queryEntity(mlpFirstDraftCollectFeedbackIdParam.getId());
    }

    @Override
    public MlpFirstDraftCollectFeedback queryEntity(String id) {
        MlpFirstDraftCollectFeedback mlpFirstDraftCollectFeedback = this.getById(id);
        if (ObjectUtil.isEmpty(mlpFirstDraftCollectFeedback)) {
            throw new CommonException("中长期规划管理-初稿意见反馈不存在，id值为：{}", id);
        }
        return mlpFirstDraftCollectFeedback;
    }

    @Override
    public List<Map<String, String>> getPlanNameMap() {
        return mlpFirstDraftCollectFeedbackMapper.getPlanNameMap(LocalDate.now().toString());
    }

    @Override
    public IPage<MlpFirstDraftCollect> pageList(MlpFirstDraftCollectFeedbackPageParam mlpFirstDraftCollectFeedbackPageParam, MlpFirstDraftCollectFeedbackQueryParam param) {
        // 分页条件
        Integer pageIndex = mlpFirstDraftCollectFeedbackPageParam.getCurrent();
        Integer pageSize = mlpFirstDraftCollectFeedbackPageParam.getSize();
        // 查询条件
        QueryWrapper<MlpFirstDraftCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("t1.delete_flag", "NOT_DELETE");
        queryWrapper.eq("t1.status", 2);
        queryWrapper.eq("t2.delete_flag", "NOT_DELETE");
        queryWrapper.apply("t1.sys_org_id LIKE CONCAT_WS('%' ,t2.sys_org_id, '%')");
        if (StringUtils.isNotEmpty(param.getPlanName())) {
            queryWrapper.like("t3.plan_name", param.getPlanName());
        }
        // 校验数据范围
        /*SysUser user = sysUserService.queryEntity(StpUtil.getLoginIdAsString());
        System.out.println("---------->" + JSON.toJSON(user));
        if (user == null) {
            throw new CommonException("获取不到当前用户,请登陆后再试");
        }*/

        queryWrapper.orderByDesc("t1.create_time");
        // 分页对象
        IPage<MlpFirstDraftCollect> page = new Page<>(pageIndex, pageSize);
        return mlpFirstDraftCollectFeedbackMapper.pageList(page, queryWrapper);
    }
}
