/*
 * 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.examine.modular.neassessmentScore.service.impl;

import cn.dev33.satoken.stp.StpUtil;
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.auth.core.util.StpClientUtil;
import com.unis.auth.modular.login.entity.UserCollectLogin;
import com.unis.examine.modular.assessmentManage.entity.AssessmentManage;
import com.unis.examine.modular.assessmentManage.param.AssessmentManagePageParam;
import com.unis.sys.modular.user.entity.SysUser;
import com.unis.sys.modular.user.service.SysUserService;
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.examine.modular.neassessmentScore.entity.BizNeAssessmentScore;
import com.unis.examine.modular.neassessmentScore.mapper.BizNeAssessmentScoreMapper;
import com.unis.examine.modular.neassessmentScore.param.BizNeAssessmentScoreAddParam;
import com.unis.examine.modular.neassessmentScore.param.BizNeAssessmentScoreEditParam;
import com.unis.examine.modular.neassessmentScore.param.BizNeAssessmentScoreIdParam;
import com.unis.examine.modular.neassessmentScore.param.BizNeAssessmentScorePageParam;
import com.unis.examine.modular.neassessmentScore.service.BizNeAssessmentScoreService;

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

/**
 * 粮食安全责任制考核-考核评分表Service接口实现类
 *
 * @author ZeroLiYi
 * @date  2024/04/23 10:15
 **/
@Service
public class BizNeAssessmentScoreServiceImpl extends ServiceImpl<BizNeAssessmentScoreMapper, BizNeAssessmentScore> implements BizNeAssessmentScoreService {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private BizNeAssessmentScoreMapper bizNeAssessmentScoreMapper;
    @Override
    public Page<BizNeAssessmentScore> page(BizNeAssessmentScorePageParam bizNeAssessmentScorePageParam) {
        QueryWrapper<BizNeAssessmentScore> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isAllNotEmpty(bizNeAssessmentScorePageParam.getSortField(), bizNeAssessmentScorePageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(bizNeAssessmentScorePageParam.getSortOrder());
            queryWrapper.orderBy(true, bizNeAssessmentScorePageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(bizNeAssessmentScorePageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(BizNeAssessmentScore::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BizNeAssessmentScoreAddParam bizNeAssessmentScoreAddParam) {
        List<BizNeAssessmentScoreEditParam> assessmentScorelist = bizNeAssessmentScoreAddParam.getAssessmentScoreList();
        for (BizNeAssessmentScoreEditParam bse:assessmentScorelist) {
            BizNeAssessmentScore bizNeAssessmentScore = BeanUtil.toBean(bse, BizNeAssessmentScore.class);
            this.save(bizNeAssessmentScore);
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BizNeAssessmentScoreEditParam bizNeAssessmentScoreEditParam) {
        List<BizNeAssessmentScoreEditParam> assessmentScorelist = bizNeAssessmentScoreEditParam.getAssessmentScoreList();
        for (BizNeAssessmentScoreEditParam bse:assessmentScorelist) {
            if(bse.getStatus()==1){
                throw new CommonException("该条考核评分数据已上传，不能进行修改");
            }
            BizNeAssessmentScore bizNeAssessmentScore = this.queryEntity(bse.getId());
            BeanUtil.copyProperties(bse, bizNeAssessmentScore);
            this.updateById(bizNeAssessmentScore);
        }
    }

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

    @Override
    public BizNeAssessmentScore detail(BizNeAssessmentScoreIdParam bizNeAssessmentScoreIdParam) {
        return this.queryEntity(bizNeAssessmentScoreIdParam.getId());
    }

    @Override
    public BizNeAssessmentScore queryEntity(String id) {
        BizNeAssessmentScore bizNeAssessmentScore = this.getById(id);
        if(ObjectUtil.isEmpty(bizNeAssessmentScore)) {
            throw new CommonException("粮食安全责任制考核-考核评分表不存在，id值为：{}", id);
        }
        return bizNeAssessmentScore;
    }

    @Override
    public IPage<Map<String,Object>> findByDetail(BizNeAssessmentScorePageParam bizNeAssessmentScorePageParam){
    //条形查询器
    QueryWrapper<BizNeAssessmentScore> queryWrapper = new QueryWrapper<>();
        SysUser user =sysUserService.queryEntity(StpUtil.getLoginIdAsString());
        if(user==null){
            throw new CommonException("获取不到当前用户，请登陆后再试");
        }
        queryWrapper.eq("nat.sys_org_id",user.getOrgId());

            queryWrapper.eq("nam.AssessmentType",1);
            queryWrapper.eq("nam.status",2);
            queryWrapper.eq("nat.assessment_manage_id",bizNeAssessmentScorePageParam.getAssessmentManageId());
    Page<BizNeAssessmentScore> p =new Page<>(bizNeAssessmentScorePageParam.getCurrent(),bizNeAssessmentScorePageParam.getSize());


    IPage<Map<String,Object>> page = bizNeAssessmentScoreMapper.selectMyMapsPage(p,queryWrapper);
        return page;
    }
}
