/*
 * 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.icPublicComplaint.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
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.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.auth.modular.login.enums.AuthExceptionEnum;
import com.unis.common.cache.CommonCacheOperator;
import com.unis.dev.api.DevConfigApi;
import com.unis.interactiveCommunication.modular.icPublicComplaint.param.*;
import com.unis.organization.modular.iodepartmentfunction.entity.BizIoDepartmentFunction;
import com.unis.organization.modular.iodepartmentfunction.mapper.BizIoDepartmentFunctionMapper;
import org.apache.commons.lang3.RandomStringUtils;
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.interactiveCommunication.modular.icPublicComplaint.entity.IcPublicComplaint;
import com.unis.interactiveCommunication.modular.icPublicComplaint.mapper.IcPublicComplaintMapper;
import com.unis.interactiveCommunication.modular.icPublicComplaint.service.IcPublicComplaintService;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.Date;
import java.util.List;

/**
 * 互动交流-信访举报表Service接口实现类
 *
 * @author hjc
 * @date 2024/05/24 14:40
 **/
@Service
public class IcPublicComplaintServiceImpl extends ServiceImpl<IcPublicComplaintMapper, IcPublicComplaint> implements IcPublicComplaintService {

    @Autowired
    private IcPublicComplaintMapper icPublicComplaintMapper;

    @Autowired
    private BizIoDepartmentFunctionMapper bizIoDepartmentFunctionMapper;

    @Override
    public Page<IcPublicComplaint> page(IcPublicComplaintPageParam icPublicComplaintPageParam) {
        QueryWrapper<IcPublicComplaint> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(icPublicComplaintPageParam.getComplaintNumber())) {
            queryWrapper.like("complaint_number", icPublicComplaintPageParam.getComplaintNumber());
        }
        if (ObjectUtil.isAllNotEmpty(icPublicComplaintPageParam.getSortField(), icPublicComplaintPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(icPublicComplaintPageParam.getSortOrder());
            queryWrapper.orderBy(true, icPublicComplaintPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(icPublicComplaintPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(IcPublicComplaint::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String add(IcPublicComplaintAddParam icPublicComplaintAddParam) {
        IcPublicComplaint icPublicComplaint = BeanUtil.toBean(icPublicComplaintAddParam, IcPublicComplaint.class);
        //提交生成 投诉编号 13位
        String complaintNumber = RandomStringUtils.random(13, true, true);
        icPublicComplaint.setComplaintNumber(complaintNumber);
        icPublicComplaint.setIsResponded(0);

        this.save(icPublicComplaint);

        return complaintNumber;

    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(IcPublicComplaintEditParam icPublicComplaintEditParam) {
        IcPublicComplaint icPublicComplaint = this.queryEntity(icPublicComplaintEditParam.getId());
        BeanUtil.copyProperties(icPublicComplaintEditParam, icPublicComplaint);

        if (icPublicComplaintEditParam.getIsResponded() == 1) {
            icPublicComplaint.setCompletionDate(new Date());
        }
        this.updateById(icPublicComplaint);
    }

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

    @Override
    public IcPublicComplaint detail(IcPublicComplaintIdParam icPublicComplaintIdParam) {
        IcPublicComplaint icPublicComplaint = this.queryEntity(icPublicComplaintIdParam.getId());
        BizIoDepartmentFunction bizIoDepartmentFunction = bizIoDepartmentFunctionMapper.selectById(icPublicComplaint.getIoDepartmentFunctionId());
        if (ObjectUtil.isEmpty(bizIoDepartmentFunction)) {
            throw new CommonException("暂无找到相关部门信息，id为{}", icPublicComplaint.getIoDepartmentFunctionId());
        }
        if (bizIoDepartmentFunction.getStatus() != 2) {
            throw new CommonException("该部门状态未被启用");
        }
        icPublicComplaint.setIoDepartmentFunctionId(bizIoDepartmentFunction.getDepartmentName());

        return icPublicComplaint;
    }

    @Override
    public IcPublicComplaint queryEntity(String id) {
        IcPublicComplaint icPublicComplaint = this.getById(id);
        if (ObjectUtil.isEmpty(icPublicComplaint)) {
            throw new CommonException("互动交流-信访举报表不存在，id值为：{}", id);
        }
        return icPublicComplaint;
    }


    /**
     * 网站-举报跟踪
     *
     * @author hjc
     * @date 2024/05/24 14:40
     */
    @Override
    public IcPublicComplaint reportTracking(ComplaintNumberParam param) {

        List<IcPublicComplaint> icPublicComplaints = icPublicComplaintMapper.selectList(Wrappers.lambdaQuery(IcPublicComplaint.class)
                .eq(IcPublicComplaint::getComplaintNumber, param.getComplaintNumber()));
        if (CollectionUtil.isEmpty(icPublicComplaints)) {
            throw new CommonException("暂无该投诉编号信息,编号为：{}", param.getComplaintNumber());

        }

        return icPublicComplaints.get(0);
    }
}
