/*
 * 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.openGovernment.modular.ogPolicyInterpretation.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.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.common.pojo.CommonValidList;
import com.unis.openGovernment.modular.ogProactiveDisclosure.param.OgGovernmentAndStatusParam;
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.openGovernment.modular.ogPolicyInterpretation.entity.OgPolicyInterpretation;
import com.unis.openGovernment.modular.ogPolicyInterpretation.mapper.OgPolicyInterpretationMapper;
import com.unis.openGovernment.modular.ogPolicyInterpretation.param.OgPolicyInterpretationAddParam;
import com.unis.openGovernment.modular.ogPolicyInterpretation.param.OgPolicyInterpretationEditParam;
import com.unis.openGovernment.modular.ogPolicyInterpretation.param.OgPolicyInterpretationIdParam;
import com.unis.openGovernment.modular.ogPolicyInterpretation.param.OgPolicyInterpretationPageParam;
import com.unis.openGovernment.modular.ogPolicyInterpretation.service.OgPolicyInterpretationService;

import java.util.List;

/**
 * 政务公开-政策解读管理表Service接口实现类
 *
 * @author hjc
 * @date 2024/05/22 10:55
 **/
@Service
public class OgPolicyInterpretationServiceImpl extends ServiceImpl<OgPolicyInterpretationMapper, OgPolicyInterpretation> implements OgPolicyInterpretationService {

    @Autowired
    private OgPolicyInterpretationMapper ogPolicyInterpretationMapper;

    @Override
    public Page<OgPolicyInterpretation> page(OgPolicyInterpretationPageParam ogPolicyInterpretationPageParam) {
        QueryWrapper<OgPolicyInterpretation> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(ogPolicyInterpretationPageParam.getTitle())) {
            queryWrapper.like("title", ogPolicyInterpretationPageParam.getTitle());
        }

        if (ObjectUtil.isAllNotEmpty(ogPolicyInterpretationPageParam.getSortField(), ogPolicyInterpretationPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(ogPolicyInterpretationPageParam.getSortOrder());
            queryWrapper.orderBy(true, ogPolicyInterpretationPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(ogPolicyInterpretationPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByDesc(OgPolicyInterpretation::getPublishTime);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(OgPolicyInterpretationAddParam ogPolicyInterpretationAddParam) {

        OgPolicyInterpretation ogPolicyInterpretation = BeanUtil.toBean(ogPolicyInterpretationAddParam, OgPolicyInterpretation.class);
        this.save(ogPolicyInterpretation);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(OgPolicyInterpretationEditParam ogPolicyInterpretationEditParam) {
        OgPolicyInterpretation ogPolicyInterpretation = this.queryEntity(ogPolicyInterpretationEditParam.getId());
        BeanUtil.copyProperties(ogPolicyInterpretationEditParam, ogPolicyInterpretation);
        this.updateById(ogPolicyInterpretation);
    }

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

    @Override
    public OgPolicyInterpretation detail(OgPolicyInterpretationIdParam ogPolicyInterpretationIdParam) {
        return this.queryEntity(ogPolicyInterpretationIdParam.getId());
    }

    @Override
    public OgPolicyInterpretation queryEntity(String id) {
        OgPolicyInterpretation ogPolicyInterpretation = this.getById(id);
        if (ObjectUtil.isEmpty(ogPolicyInterpretation)) {
            throw new CommonException("政务公开-政策解读管理表不存在，id值为：{}", id);
        }
        return ogPolicyInterpretation;
    }

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

        for (OgGovernmentAndStatusParam ogGovernmentAndStatusParam : paramCommonValidList) {
            OgPolicyInterpretation ogPolicyInterpretation = this.queryEntity(ogGovernmentAndStatusParam.getId());
            ogPolicyInterpretation.setStatus(ogGovernmentAndStatusParam.getStatus());

            this.updateById(ogPolicyInterpretation);
        }
    }

    /**
     * 网站-获取政策解读-文字解读
     *
     * @author hjc
     * @date 2024/05/22 10:55
     */
    @Override
    public Page<OgPolicyInterpretation> getTextualInterpretationAll(OgPolicyInterpretationPageParam ogPolicyInterpretationPageParam) {
        QueryWrapper<OgPolicyInterpretation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OgPolicyInterpretation::getInterpretationCategory,1);
        queryWrapper.lambda().eq(OgPolicyInterpretation::getStatus, 2);
        if (ObjectUtil.isAllNotEmpty(ogPolicyInterpretationPageParam.getSortField(), ogPolicyInterpretationPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(ogPolicyInterpretationPageParam.getSortOrder());
            queryWrapper.orderBy(true, ogPolicyInterpretationPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(ogPolicyInterpretationPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByDesc(OgPolicyInterpretation::getCreateTime);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    /**
     * 网站-获取政策解读-图文解读
     *
     * @author hjc
     * @date  2024/05/22 10:55
     */
    @Override
    public Page<OgPolicyInterpretation> getPictureInterpretationAll(OgPolicyInterpretationPageParam ogPolicyInterpretationPageParam) {
        QueryWrapper<OgPolicyInterpretation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OgPolicyInterpretation::getInterpretationCategory,2);
        queryWrapper.lambda().eq(OgPolicyInterpretation::getStatus, 2);
        if (ObjectUtil.isAllNotEmpty(ogPolicyInterpretationPageParam.getSortField(), ogPolicyInterpretationPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(ogPolicyInterpretationPageParam.getSortOrder());
            queryWrapper.orderBy(true, ogPolicyInterpretationPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(ogPolicyInterpretationPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByDesc(OgPolicyInterpretation::getCreateTime);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }
}
