package com.yida.data.school.transaction.service.impl;

import cc.mrbird.febs.common.redis.service.RedisService;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.yida.data.common.core.entity.apply.EduApply;
import com.yida.data.common.core.entity.apply.EduApplyFunction;
import com.yida.data.common.core.entity.constant.FebsConstant;
import com.yida.data.common.core.utils.Asserts;
import com.yida.data.school.transaction.mapper.EduApplyFunctionMapper;
import com.yida.data.school.transaction.mapper.EduApplyMapper;
import com.yida.data.school.transaction.service.EduAppService;
import com.yida.data.school.transaction.service.EduApplyFunctionService;
import com.yida.data.school.vo.transcation.EduApplyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class EduAppServiceImpl extends ServiceImpl<EduApplyFunctionMapper, EduApplyFunction> implements EduAppService {

    @Resource
    private EduApplyMapper eduApplyMapper;

    @Resource
    private EduApplyFunctionService eduApplyFunctionService;
    @Autowired
    private RedisService redisService;


    /**
     * 分页查询
     *
     * @param
     * @return
     */
    @Override
    public IPage<EduApply> selectPageEduApplyService(String name, Page page) {

        return eduApplyMapper.listEduProductApply(page, name);
    }

    /**
     * 新增应用，批量新增功能
     *
     * @param
     * @return
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertEduApplyService(EduApplyVO eduApplyVo) {
        EduApply eduApply = eduApplyVo.getEduApply();
        Asserts.isTrue(eduApplyMapper.selectCount(Wrappers.lambdaQuery(new EduApply())
                .eq(EduApply::getApplyCode, eduApply.getApplyCode())) == 0, "该应用编码已存在");
        Asserts.isTrue(eduApplyMapper.selectCount(Wrappers.lambdaQuery(new EduApply())
                .eq(EduApply::getApplyName, eduApply.getApplyName())) == 0, "该应用名称已存在");
        eduApplyMapper.insert(eduApply);
        List<EduApplyFunction> functionNameList = eduApplyVo.getFunctionList();
        functionNameList.forEach(eduApplyFunction -> eduApplyFunction.setApplyId(eduApply.getId()));
        eduApplyFunctionService.saveBatch(functionNameList);
        redisService.del(FebsConstant.EDU_APPLY);

    }

    /**
     * 修改应用
     *
     * @param eduApplyVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void upDataApplyService(EduApplyVO eduApplyVo) {
        EduApply eduApply = eduApplyVo.getEduApply();
        Asserts.isTrue(eduApplyMapper.selectCount(Wrappers.lambdaQuery(new EduApply())
                .eq(EduApply::getApplyCode, eduApply.getApplyCode())
                .ne(EduApply::getId, eduApply.getId())) == 0, "该应用编码已存在");
        Asserts.isTrue(eduApplyMapper.selectCount(Wrappers.lambdaQuery(new EduApply())
                .eq(EduApply::getApplyName, eduApply.getApplyName())
                .ne(EduApply::getId, eduApply.getId())) == 0, "该应用名称已存在");
        eduApplyMapper.updateById(eduApply);
        List<EduApplyFunction> functionNameList = eduApplyVo.getFunctionList();
        functionNameList.forEach(eduApplyFunction -> eduApplyFunction.setApplyId(eduApply.getId()));
        eduApplyFunctionService.saveOrUpdateBatch(functionNameList);
        redisService.del(FebsConstant.EDU_APPLY);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeEduApply(Long id) {
        eduApplyMapper.deleteById(id);
    }


}
