/*
 * 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.organization.modular.iodepartmentfunction.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.importantNews.modular.sinstatecouncilnotice.entity.BizSinStateCouncilNotice;
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.organization.modular.iodepartmentfunction.entity.BizIoDepartmentFunction;
import com.unis.organization.modular.iodepartmentfunction.mapper.BizIoDepartmentFunctionMapper;
import com.unis.organization.modular.iodepartmentfunction.param.BizIoDepartmentFunctionAddParam;
import com.unis.organization.modular.iodepartmentfunction.param.BizIoDepartmentFunctionEditParam;
import com.unis.organization.modular.iodepartmentfunction.param.BizIoDepartmentFunctionIdParam;
import com.unis.organization.modular.iodepartmentfunction.param.BizIoDepartmentFunctionPageParam;
import com.unis.organization.modular.iodepartmentfunction.service.BizIoDepartmentFunctionService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 机构概况-职能信息管理表Service接口实现类
 *
 * @author shine
 * @date  2024/05/21 14:25
 **/
@Service
public class BizIoDepartmentFunctionServiceImpl extends ServiceImpl<BizIoDepartmentFunctionMapper, BizIoDepartmentFunction> implements BizIoDepartmentFunctionService {

    @Resource
    private BizIoDepartmentFunctionMapper bizIoDepartmentFunctionMapper;
    @Override
    public Page<BizIoDepartmentFunction> page(BizIoDepartmentFunctionPageParam bizIoDepartmentFunctionPageParam) {
        QueryWrapper<BizIoDepartmentFunction> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(bizIoDepartmentFunctionPageParam.getDepartmentName())) {
            queryWrapper.lambda().like(BizIoDepartmentFunction::getDepartmentName, bizIoDepartmentFunctionPageParam.getDepartmentName());
        }
        if(ObjectUtil.isNotEmpty(bizIoDepartmentFunctionPageParam.getOrganizationCategory())) {
            queryWrapper.lambda().eq(BizIoDepartmentFunction::getOrganizationCategory, bizIoDepartmentFunctionPageParam.getOrganizationCategory());
        }
        if(ObjectUtil.isNotEmpty(bizIoDepartmentFunctionPageParam.getLeaderName())) {
            queryWrapper.lambda().like(BizIoDepartmentFunction::getLeaderName, bizIoDepartmentFunctionPageParam.getLeaderName());
        }
        if(ObjectUtil.isNotEmpty(bizIoDepartmentFunctionPageParam.getStatus())) {
            queryWrapper.lambda().eq(BizIoDepartmentFunction::getStatus, bizIoDepartmentFunctionPageParam.getStatus());
        }
        if(ObjectUtil.isAllNotEmpty(bizIoDepartmentFunctionPageParam.getSortField(), bizIoDepartmentFunctionPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(bizIoDepartmentFunctionPageParam.getSortOrder());
            queryWrapper.orderBy(true, bizIoDepartmentFunctionPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(bizIoDepartmentFunctionPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByDesc(BizIoDepartmentFunction::getCreateTime);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BizIoDepartmentFunctionAddParam bizIoDepartmentFunctionAddParam) {
        BizIoDepartmentFunction bizIoDepartmentFunction = BeanUtil.toBean(bizIoDepartmentFunctionAddParam, BizIoDepartmentFunction.class);
        this.save(bizIoDepartmentFunction);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BizIoDepartmentFunctionEditParam bizIoDepartmentFunctionEditParam) {
        BizIoDepartmentFunction bizIoDepartmentFunction = this.queryEntity(bizIoDepartmentFunctionEditParam.getId());
        BeanUtil.copyProperties(bizIoDepartmentFunctionEditParam, bizIoDepartmentFunction);
        this.updateById(bizIoDepartmentFunction);
    }

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

    @Override
    public BizIoDepartmentFunction detail(BizIoDepartmentFunctionIdParam bizIoDepartmentFunctionIdParam) {
        return this.queryEntity(bizIoDepartmentFunctionIdParam.getId());
    }

    @Override
    public BizIoDepartmentFunction queryEntity(String id) {
        BizIoDepartmentFunction bizIoDepartmentFunction = this.getById(id);
        if(ObjectUtil.isEmpty(bizIoDepartmentFunction)) {
            throw new CommonException("机构概况-职能信息管理表不存在，id值为：{}", id);
        }
        return bizIoDepartmentFunction;
    }

    @Override
    public void batchUpdateStatus(List<BizIoDepartmentFunction> list) {

        for(BizIoDepartmentFunction bizIoDepartmentFunction:list){
            this.updateById(bizIoDepartmentFunction);
        }

    }

    @Override
    public Map<Integer, List<BizIoDepartmentFunction>> getList() {

        QueryWrapper<BizIoDepartmentFunction> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BizIoDepartmentFunction::getStatus, 2);//启用
        List<BizIoDepartmentFunction> bizIoDepartmentFunctionList = this.list(queryWrapper);

        return bizIoDepartmentFunctionList.stream().collect(Collectors.groupingBy(BizIoDepartmentFunction::getOrganizationCategory));
    }
}
