/*
 * 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.alFileRegistration.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.basic.info.modular.grainPriceCollectCitySum.entity.GrainPriceCollectCitySum;
import com.unis.policy.info.modular.alPolicyFileReport.entity.AlPolicyFileReport;
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.alFileRegistration.entity.AlFileRegistration;
import com.unis.policy.info.modular.alFileRegistration.mapper.AlFileRegistrationMapper;
import com.unis.policy.info.modular.alFileRegistration.param.AlFileRegistrationAddParam;
import com.unis.policy.info.modular.alFileRegistration.param.AlFileRegistrationEditParam;
import com.unis.policy.info.modular.alFileRegistration.param.AlFileRegistrationIdParam;
import com.unis.policy.info.modular.alFileRegistration.param.AlFileRegistrationPageParam;
import com.unis.policy.info.modular.alFileRegistration.service.AlFileRegistrationService;

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

/**
 * 行政法规管理-文件备案Service接口实现类
 *
 * @author TS
 * @date  2024/04/18 14:59
 **/
@Service
public class AlFileRegistrationServiceImpl extends ServiceImpl<AlFileRegistrationMapper, AlFileRegistration> implements AlFileRegistrationService {

    @Resource
    private  AlFileRegistrationMapper alFileRegistrationMapper;
    @Override
    public Page<AlFileRegistration> page(AlFileRegistrationPageParam alFileRegistrationPageParam) {
        QueryWrapper<AlFileRegistration> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isAllNotEmpty(alFileRegistrationPageParam.getSortField(), alFileRegistrationPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(alFileRegistrationPageParam.getSortOrder());
            queryWrapper.orderBy(true, alFileRegistrationPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(alFileRegistrationPageParam.getSortField()));
        } else {
            //默认使用创建时间倒序
            queryWrapper.lambda().orderByDesc(AlFileRegistration::getCreateTime);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(AlFileRegistrationAddParam alFileRegistrationAddParam) {
        AlFileRegistration alFileRegistration = BeanUtil.toBean(alFileRegistrationAddParam, AlFileRegistration.class);
        this.save(alFileRegistration);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(AlFileRegistrationEditParam alFileRegistrationEditParam) {
        AlFileRegistration alFileRegistration = this.queryEntity(alFileRegistrationEditParam.getId());
        BeanUtil.copyProperties(alFileRegistrationEditParam, alFileRegistration);
        this.updateById(alFileRegistration);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(AlFileRegistrationIdParam alFileRegistrationIdParam) {
        this.queryEntity(alFileRegistrationIdParam.getId());
        // 执行删除
        this.removeById(alFileRegistrationIdParam.getId());
    }

    @Override
    public AlFileRegistration detail(AlFileRegistrationIdParam alFileRegistrationIdParam) {
        return this.queryEntity(alFileRegistrationIdParam.getId());
    }

    @Override
    public AlFileRegistration queryEntity(String id) {
        AlFileRegistration alFileRegistration = this.getById(id);
        if(ObjectUtil.isEmpty(alFileRegistration)) {
            throw new CommonException("行政法规管理-文件备案不存在，id值为：{}", id);
        }
        return alFileRegistration;
    }

    /**
     * 获取行政法规管理-文件备案自定义分页
     * @param pageParam
     * @return
     */
    @Override
    public IPage<Map<String, Object>> findPage(AlFileRegistrationPageParam pageParam) {
        //条形查询器
        QueryWrapper<AlFileRegistration> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(pageParam.getRegistrant())){
            queryWrapper.like("a.registrant",pageParam.getRegistrant());
        }
        if (StringUtils.isNotBlank(pageParam.getSysOrgName())){
            queryWrapper.like("b.name",pageParam.getSysOrgName());
        }

        Page<GrainPriceCollectCitySum> p =new Page<>(pageParam.getCurrent(),pageParam.getSize());
        IPage<Map<String,Object>> page = alFileRegistrationMapper.selectMyMapsPage(p,queryWrapper);
        return page;
    }
}
