/*
 * 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.interactiveCommunication.modular.icPublicOpinionCollect.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.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.common.enums.CommonSortOrderEnum;
import com.unis.common.page.CommonPageRequest;
import com.unis.common.pojo.CommonValidList;
import com.unis.interactiveCommunication.modular.icPublicOpinionCollect.param.*;
import com.unis.interactiveCommunication.modular.icPublicOpinionCollect.vo.IcPublicOpinionCollectVO;
import com.unis.interactiveCommunication.modular.icPublicOpinionCollectMessage.entity.IcPublicOpinionCollectMessage;
import com.unis.interactiveCommunication.modular.icPublicOpinionCollectMessage.mapper.IcPublicOpinionCollectMessageMapper;
import com.unis.openGovernment.modular.ogAdministrativeDocument.param.OgAdministrativeDocumentAndStatusParam;
import com.unis.organization.modular.iodepartmentfunction.entity.BizIoDepartmentFunction;
import com.unis.organization.modular.iodepartmentfunction.mapper.BizIoDepartmentFunctionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.unis.common.exception.CommonException;
import com.unis.interactiveCommunication.modular.icPublicOpinionCollect.entity.IcPublicOpinionCollect;
import com.unis.interactiveCommunication.modular.icPublicOpinionCollect.mapper.IcPublicOpinionCollectMapper;
import com.unis.interactiveCommunication.modular.icPublicOpinionCollect.service.IcPublicOpinionCollectService;

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

/**
 * 互动交流-民意征集管理表Service接口实现类
 *
 * @author hjc
 * @date 2024/05/23 19:44
 **/
@Service
public class IcPublicOpinionCollectServiceImpl extends ServiceImpl<IcPublicOpinionCollectMapper, IcPublicOpinionCollect> implements IcPublicOpinionCollectService {

    @Autowired
    private IcPublicOpinionCollectMapper icPublicOpinionCollectMapper;

    @Autowired
    private BizIoDepartmentFunctionMapper bizIoDepartmentFunctionMapper;

    @Autowired
    private IcPublicOpinionCollectMessageMapper icPublicOpinionCollectMessageMapper;

    @Override
    public Page<IcPublicOpinionCollectVO> page(IcPublicOpinionCollectPageParam pageParam) {
      /*  QueryWrapper<IcPublicOpinionCollect> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(icPublicOpinionCollectPageParam.getCollectionTitle())) {
            queryWrapper.like("collection_title", icPublicOpinionCollectPageParam.getCollectionTitle());
        }
        if (ObjectUtil.isNotEmpty(icPublicOpinionCollectPageParam.getProgressStatus())) {
            queryWrapper.eq("progress_status", icPublicOpinionCollectPageParam.getProgressStatus());
        }
        if (ObjectUtil.isAllNotEmpty(icPublicOpinionCollectPageParam.getSortField(), icPublicOpinionCollectPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(icPublicOpinionCollectPageParam.getSortOrder());
            queryWrapper.orderBy(true, icPublicOpinionCollectPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(icPublicOpinionCollectPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(IcPublicOpinionCollect::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);*/

        Page<IcPublicOpinionCollectVO> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        /* return bizIoDepartmentFunctionMapper.selectMyPage(page, pageParam.getCollectionTitle(), pageParam.getProgressStatus());*/
        return icPublicOpinionCollectMapper.selectMyPage(page, pageParam.getCollectionTitle(), pageParam.getProgressStatus());

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(IcPublicOpinionCollectAddParam icPublicOpinionCollectAddParam) {

        IcPublicOpinionCollect icPublicOpinionCollect = BeanUtil.toBean(icPublicOpinionCollectAddParam, IcPublicOpinionCollect.class);
        if (icPublicOpinionCollectAddParam.getStatus() == 2) {
            //查看时间是否开始
            Date startTime = icPublicOpinionCollect.getStartTime();
            Date endTime = icPublicOpinionCollect.getEndTime();
            Date date = new Date();
            if (date.compareTo(startTime) == 0 || startTime.before(date)) {
                icPublicOpinionCollect.setProgressStatus(1);
            }
            if (date.before(startTime)){
                icPublicOpinionCollect.setProgressStatus(0);
            }
            if ( date.after(endTime)) {
                icPublicOpinionCollect.setProgressStatus(2);
            }
        }
        this.save(icPublicOpinionCollect);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(IcPublicOpinionCollectEditParam icPublicOpinionCollectEditParam) {

        IcPublicOpinionCollect icPublicOpinionCollect = this.queryEntity(icPublicOpinionCollectEditParam.getId());
        BeanUtil.copyProperties(icPublicOpinionCollectEditParam, icPublicOpinionCollect);
        if (icPublicOpinionCollectEditParam.getStatus() == 2) {
            //查看时间是否开始
            Date startTime = icPublicOpinionCollect.getStartTime();
            Date date = new Date();
            if (date.compareTo(startTime) == 0 || startTime.before(date)) {
                icPublicOpinionCollect.setProgressStatus(1);
            }
        }
        this.updateById(icPublicOpinionCollect);
    }

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

    @Override
    public JSONObject detail(IcPublicOpinionCollectIdParam icPublicOpinionCollectIdParam) {

        JSONObject jsonObject = new JSONObject();

        IcPublicOpinionCollect icPublicOpinionCollect = icPublicOpinionCollectMapper.selectById(icPublicOpinionCollectIdParam.getId());
        if (ObjectUtil.isEmpty(icPublicOpinionCollect)) {
            throw new CommonException("暂无对应数据，id为{}", icPublicOpinionCollectIdParam.getId());
        }
        jsonObject.put("icPublicOpinionCollect", icPublicOpinionCollect);

        //意见征集数据
        List<IcPublicOpinionCollectMessage> icPublicOpinionCollectMessages = icPublicOpinionCollectMessageMapper.selectList(Wrappers.lambdaQuery(IcPublicOpinionCollectMessage.class)
                .eq(IcPublicOpinionCollectMessage::getPublicOpinionCollectId, icPublicOpinionCollect.getId()));
        jsonObject.put("icPublicOpinionCollectMessages", icPublicOpinionCollectMessages);

        return jsonObject;

    }

    @Override
    public IcPublicOpinionCollect queryEntity(String id) {
        IcPublicOpinionCollect icPublicOpinionCollect = this.getById(id);
        if (ObjectUtil.isEmpty(icPublicOpinionCollect)) {
            throw new CommonException("互动交流-民意征集管理表不存在，id值为：{}", id);
        }
        return icPublicOpinionCollect;
    }

    /**
     * 获取征集部门信息
     *
     * @author hjc
     * @date 2024/05/23 19:44
     */
    @Override
    public List<BizIoDepartmentFunction> getIoDepartmentFunction() {
        return bizIoDepartmentFunctionMapper.selectList(Wrappers.lambdaQuery(BizIoDepartmentFunction.class)
                .eq(BizIoDepartmentFunction::getStatus, 2));
    }

    /**
     * 启用，禁用
     *
     * @author hjc
     * @date 2024/05/20 16:18
     */
    @Override
    public void enableAndDisable(CommonValidList<OgAdministrativeDocumentAndStatusParam> paramCommonValidList) {

        for (OgAdministrativeDocumentAndStatusParam documentAndStatusParam : paramCommonValidList) {

            IcPublicOpinionCollect icPublicOpinionCollect = this.queryEntity(documentAndStatusParam.getId());
            icPublicOpinionCollect.setStatus(documentAndStatusParam.getStatus());

            if (documentAndStatusParam.getStatus() == 2) {
                //查看时间是否开始
                Date startTime = icPublicOpinionCollect.getStartTime();
                Date endTime = icPublicOpinionCollect.getEndTime();
                Date date = new Date();
                if (date.compareTo(startTime) == 0 || startTime.before(date) && endTime.after(date)) {
                    icPublicOpinionCollect.setProgressStatus(1);
                }
                if (date.after(endTime)){
                    throw new CommonException("已结束收集，不能进行启用");
                }
            }
            if (documentAndStatusParam.getStatus() == 3) {
                //查看时间是否开始
                Date startTime = icPublicOpinionCollect.getStartTime();
                Date endTime = icPublicOpinionCollect.getEndTime();
                Date date = new Date();
                if (startTime.after(date)) {
                    icPublicOpinionCollect.setProgressStatus(0);
                }
                if (endTime.before(date)){
                    icPublicOpinionCollect.setProgressStatus(2);
                }
            }
            this.updateById(icPublicOpinionCollect);

        }
    }

    /**
     * 网站-分页展示
     *
     * @author hjc
     * @date 2024/05/23 19:44
     */
    @Override
    public Page<IcPublicOpinionCollect> getSitePage(IcPublicOpinionCollectPageParam icPublicOpinionCollectPageParam) {
        QueryWrapper<IcPublicOpinionCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 2);
        if (ObjectUtil.isAllNotEmpty(icPublicOpinionCollectPageParam.getSortField(), icPublicOpinionCollectPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(icPublicOpinionCollectPageParam.getSortOrder());
            queryWrapper.orderBy(true, icPublicOpinionCollectPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(icPublicOpinionCollectPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByDesc(IcPublicOpinionCollect::getCreateTime);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }
}
