package com.yida.data.school.transaction.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.apply.EduApply;
import com.yida.data.common.core.entity.apply.EduProductApply;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.school.transaction.mapper.EduProductApplyMapper;
import com.yida.data.school.transaction.service.EduAppService;
import com.yida.data.school.vo.transcation.EduApplyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 后台应用管理
 */
@Api(tags = "交易-应用")
@RestController
@RequiredArgsConstructor
@RequestMapping("/transaction/apply")
public class EduApplyController {
    private final EduAppService eduAppService;
    private final EduProductApplyMapper eduProductApplyMapper;


    @ApiOperation("应用分页查询")
    @GetMapping("/listApply")
    public ResultBean<IPage<EduApply>> listApply(@ApiParam("app名字") @RequestParam String applyName,
                                                 @ApiParam("当前页码") @RequestParam(defaultValue = "1") Integer pageNum,
                                                 @ApiParam("页面大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<EduApply> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        return ResultBean.buildSuccess(eduAppService.selectPageEduApplyService(applyName, page));
    }

    @ApiOperation("新增应用")
    @PostMapping("/insertApply")
    @OperationLog(module = ModuleName.SCHOOL, methods = "新增应用", type = OperationLogTypeEnum.INSERT)
    public ResultBean insertApply(@ApiParam("应用名字") @RequestBody EduApplyVO applyFunction) {
        eduAppService.insertEduApplyService(applyFunction);
        return ResultBean.buildSuccess();
    }


    @ApiOperation("修改应用")
    @PostMapping("/updateApply")
    @OperationLog(module = ModuleName.SCHOOL, methods = "修改应用", type = OperationLogTypeEnum.UPDATE)
    public ResultBean upDataApply(@ApiParam("应用名字") @RequestBody EduApplyVO eduApply) {
        eduAppService.upDataApplyService(eduApply);
        return ResultBean.buildSuccess();
    }


    @ApiOperation("删除应用")
    @GetMapping("/removeEduApply")
    @OperationLog(module = ModuleName.SCHOOL, methods = "删除应用", type = OperationLogTypeEnum.DELETE)
    public ResultBean removeEduApply(@ApiParam("应用id") @RequestParam Long applyId) {

        List<EduProductApply> eduProductApplies = eduProductApplyMapper.selectList(Wrappers.lambdaQuery(new EduProductApply())
                .eq(EduProductApply::getApplyId, applyId));

        //判断应用中是否有关联商品；
        if (ObjectUtil.isEmpty(eduProductApplies)) {

            //没有应用就删除
            eduAppService.removeEduApply(applyId);

            return ResultBean.buildSuccess();
        }
        return ResultBean.buildError("应用中关联商品", eduProductApplies);
    }

}
