/*
 * 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.mlpFirstDraftCollect.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.policy.info.modular.mlpFirstDraft.mapper.MlpFirstDraftMapper;
import com.unis.policy.info.modular.mlpFirstDraftCollect.param.*;
import com.unis.policy.info.modular.mlpFirstDraftCollectFeedback.param.MlpFirstDraftCollectFeedbackAddParam;
import com.unis.policy.info.modular.mlpFirstDraftCollectOrg.entity.MlpFirstDraftCollectOrg;
import com.unis.policy.info.modular.mlpFirstDraftCollectOrg.mapper.MlpFirstDraftCollectOrgMapper;
import lombok.extern.slf4j.Slf4j;
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.mlpFirstDraftCollect.entity.MlpFirstDraftCollect;
import com.unis.policy.info.modular.mlpFirstDraftCollect.mapper.MlpFirstDraftCollectMapper;
import com.unis.policy.info.modular.mlpFirstDraftCollect.service.MlpFirstDraftCollectService;

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

/**
 * 中长期规划管理-初稿意见征集Service接口实现类
 *
 * @author QSHan
 * @date 2024/04/18 20:02
 **/
@Slf4j
@Service
public class MlpFirstDraftCollectServiceImpl extends ServiceImpl<MlpFirstDraftCollectMapper, MlpFirstDraftCollect> implements MlpFirstDraftCollectService {
    /*@Resource
    private MlpFirstDraftMapper mlpFirstDraftMapper;*/
    @Resource
    private MlpFirstDraftCollectOrgMapper mlpFirstDraftCollectOrgMapper;
    @Resource
    private MlpFirstDraftCollectMapper mlpFirstDraftCollectMapper;

    @Override
    public IPage<MlpFirstDraftCollect> page(MlpFirstDraftCollectPageParam mlpFirstDraftCollectPageParam, MlpFirstDraftCollectQueryParam param) {
        // 分页条件
        Integer pageIndex = mlpFirstDraftCollectPageParam.getCurrent();
        Integer pageSize = mlpFirstDraftCollectPageParam.getSize();
        // 查询条件
        QueryWrapper<MlpFirstDraftCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("t1.delete_flag", "NOT_DELETE");
        if (StringUtils.isNotEmpty(param.getPlanName())) {
            queryWrapper.like("t2.plan_name", param.getPlanName());
        }
        queryWrapper.orderByDesc("t1.create_time");
        // 分页对象
        IPage<MlpFirstDraftCollect> page = new Page<>(pageIndex, pageSize);
        return mlpFirstDraftCollectMapper.pageList(page, queryWrapper);


        /*QueryWrapper<MlpFirstDraftCollect> queryWrapper = new QueryWrapper<>();
        // 关联表查询条件
        if (StringUtils.isNotBlank(param.getPlanName())) {
            // 规划初稿,规划名称模糊查询,询所有匹配关联id
            List<String> mlpFirstDrafts = mlpFirstDraftMapper.resultIdList(param.getPlanName());
            if (!mlpFirstDrafts.isEmpty()) {
                queryWrapper.in("first_draft_id", mlpFirstDrafts);
            }
        }
        if (ObjectUtil.isAllNotEmpty(mlpFirstDraftCollectPageParam.getSortField(), mlpFirstDraftCollectPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(mlpFirstDraftCollectPageParam.getSortOrder());
            queryWrapper.orderBy(true, mlpFirstDraftCollectPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(mlpFirstDraftCollectPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(MlpFirstDraftCollect::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);*/
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(MlpFirstDraftCollectAddParam mlpFirstDraftCollectAddParam) {
        // 保存本业务表数据
        MlpFirstDraftCollect mlpFirstDraftCollect = BeanUtil.toBean(mlpFirstDraftCollectAddParam, MlpFirstDraftCollect.class);
        // 状态 1:保存;2:发布
        Integer status = mlpFirstDraftCollect.getStatus();
        if (status == 1) {
            this.save(mlpFirstDraftCollect);
        } else if (status == 2) { // 是否多选多个处室
            this.save(mlpFirstDraftCollect);
            // 保存关联收集处室sys_org数据
            String sysOrgId = mlpFirstDraftCollect.getSysOrgId();
            this.insertCollectOrg(sysOrgId, mlpFirstDraftCollect, mlpFirstDraftCollectAddParam.getPlanName());
        } else {
            throw new CommonException("status parameter cannot be null!");
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(MlpFirstDraftCollectEditParam mlpFirstDraftCollectEditParam) {
        MlpFirstDraftCollect mlpFirstDraftCollect = this.queryEntity(mlpFirstDraftCollectEditParam.getId());
        BeanUtil.copyProperties(mlpFirstDraftCollectEditParam, mlpFirstDraftCollect);

        // 状态 1:保存;2:发布
        Integer status = mlpFirstDraftCollect.getStatus();
        if (status == 1) {
            this.updateById(mlpFirstDraftCollect);
        } else if (status == 2) { // 是否多选多个处室
            this.updateById(mlpFirstDraftCollect);
            // 保存关联收集处室sys_org数据
            String sysOrgId = mlpFirstDraftCollect.getSysOrgId();
            this.insertCollectOrg(sysOrgId, mlpFirstDraftCollect, mlpFirstDraftCollectEditParam.getPlanName());
        } else {
            throw new CommonException("status parameter cannot be null!");
        }

    }

    /**
     * 新增,修改子表封装方法
     * @param sysOrgId 机构编码
     * @param mlpFirstDraftCollect 数据对象
     * @param planName 规划名称
     */
    private void insertCollectOrg(String sysOrgId, MlpFirstDraftCollect mlpFirstDraftCollect, String planName) {
        // 截取
        String[] split = sysOrgId.split(",");
        for (String s : split) {
            MlpFirstDraftCollectOrg OrgAddParam = BeanUtil.toBean(mlpFirstDraftCollect, MlpFirstDraftCollectOrg.class);
            OrgAddParam.setId(null);
            OrgAddParam.setFirstDraftCollectId(mlpFirstDraftCollect.getId());
            OrgAddParam.setSysOrgId(s);
            OrgAddParam.setPlanName(planName);
            mlpFirstDraftCollectOrgMapper.insert(OrgAddParam);
        }
    }

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

    @Override
    public MlpFirstDraftCollect detail(MlpFirstDraftCollectIdParam mlpFirstDraftCollectIdParam) {
        return this.queryEntity(mlpFirstDraftCollectIdParam.getId());
    }

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

    @Override
    public List<MlpFirstDraftCollectFeedbackAddParam> checkList(String id) {
        return mlpFirstDraftCollectMapper.checkList(id);
    }
}
