/*
 * 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.ogOpenInfoRequest.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.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.openGovernment.modular.ogOpenInfoRequest.param.*;
import org.apache.commons.lang3.RandomStringUtils;
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.ogOpenInfoRequest.entity.OgOpenInfoRequest;
import com.unis.openGovernment.modular.ogOpenInfoRequest.mapper.OgOpenInfoRequestMapper;
import com.unis.openGovernment.modular.ogOpenInfoRequest.service.OgOpenInfoRequestService;

import java.util.List;

/**
 * 政务公开-依公开申请管理表Service接口实现类
 *
 * @author hjc
 * @date 2024/05/22 16:19
 **/
@Service
public class OgOpenInfoRequestServiceImpl extends ServiceImpl<OgOpenInfoRequestMapper, OgOpenInfoRequest> implements OgOpenInfoRequestService {

    @Autowired
    private OgOpenInfoRequestMapper ogOpenInfoRequestMapper;

    @Override
    public Page<OgOpenInfoRequest> page(OgOpenInfoRequestPageParam ogOpenInfoRequestPageParam) {
        QueryWrapper<OgOpenInfoRequest> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(ogOpenInfoRequestPageParam.getIdentificationNumber())) {
            queryWrapper.like("identification_number", ogOpenInfoRequestPageParam.getIdentificationNumber());
        }
        if (StringUtils.isNotEmpty(ogOpenInfoRequestPageParam.getName())) {
            queryWrapper.like("name", ogOpenInfoRequestPageParam.getName());
        }
        if (ObjectUtil.isAllNotEmpty(ogOpenInfoRequestPageParam.getSortField(), ogOpenInfoRequestPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(ogOpenInfoRequestPageParam.getSortOrder());
            queryWrapper.orderBy(true, ogOpenInfoRequestPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(ogOpenInfoRequestPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(OgOpenInfoRequest::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public FindOgOpenInfoRequestParam add(OgOpenInfoRequestAddParam ogOpenInfoRequestAddParam) {

        OgOpenInfoRequest ogOpenInfoRequest = BeanUtil.toBean(ogOpenInfoRequestAddParam, OgOpenInfoRequest.class);

        //回复就生成查询码 13位 和密码6位
        String queryCode = RandomStringUtils.random(13, true, true);
        String queryPassword = RandomStringUtils.random(6, true, true);
        ogOpenInfoRequest.setQueryCode(queryCode);
        ogOpenInfoRequest.setQueryPassword(queryPassword);

        this.save(ogOpenInfoRequest);

        FindOgOpenInfoRequestParam findOgOpenInfoRequestParam = new FindOgOpenInfoRequestParam();
        findOgOpenInfoRequestParam.setQueryCode(queryCode);
        findOgOpenInfoRequestParam.setQueryPassword(queryPassword);
        return findOgOpenInfoRequestParam;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(OgOpenInfoRequestEditParam ogOpenInfoRequestEditParam) {
        OgOpenInfoRequest ogOpenInfoRequest = this.queryEntity(ogOpenInfoRequestEditParam.getId());
        BeanUtil.copyProperties(ogOpenInfoRequestEditParam, ogOpenInfoRequest);

        this.updateById(ogOpenInfoRequest);


    }

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

    @Override
    public OgOpenInfoRequest detail(OgOpenInfoRequestIdParam ogOpenInfoRequestIdParam) {
        return this.queryEntity(ogOpenInfoRequestIdParam.getId());
    }

    @Override
    public OgOpenInfoRequest queryEntity(String id) {
        OgOpenInfoRequest ogOpenInfoRequest = this.getById(id);
        if (ObjectUtil.isEmpty(ogOpenInfoRequest)) {
            throw new CommonException("政务公开-依公开申请管理表不存在，id值为：{}", id);
        }
        return ogOpenInfoRequest;
    }

    /**
     * 查询页面
     *
     * @author hjc
     * @date 2024/05/22 16:19
     */
    @Override
    public OgOpenInfoRequest find(FindOgOpenInfoRequestParam findOgOpenInfoRequestParam) {
        LambdaQueryWrapper<OgOpenInfoRequest> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(findOgOpenInfoRequestParam.getQueryCode())) {
            wrapper.like(OgOpenInfoRequest::getQueryCode, findOgOpenInfoRequestParam.getQueryCode());
        }
        if (StringUtils.isNotEmpty(findOgOpenInfoRequestParam.getQueryPassword())) {
            wrapper.like(OgOpenInfoRequest::getQueryPassword, findOgOpenInfoRequestParam.getQueryPassword());
        }
        return ogOpenInfoRequestMapper.selectOne(wrapper);

    }
}
