package com.bc.exam.modules.tsjxxb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bc.exam.core.api.ApiRest;
import com.bc.exam.core.api.controller.BaseController;
import com.bc.exam.core.api.dto.BaseIdsReqDTO;
import com.bc.exam.core.api.dto.BaseStateReqDTO;
import com.bc.exam.core.api.dto.PagingReqDTO;
import com.bc.exam.core.exception.ServiceException;
import com.bc.exam.modules.tsjxxb.entity.TSjxxb;
import com.bc.exam.modules.tsjxxb.service.TSjxxbService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description 描述:书籍信息表前端控制器
 * @Author A贾宇婷034244310
 * @Date 20260615
 */
@RestController
@RequestMapping("/tsjxxb/controller")
public class TSjxxbController extends BaseController {

    @Resource
    TSjxxbService sjxxbService;

    /**
     * 添加或修改
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "添加或修改")
    @PostMapping("/save")
    public ApiRest save(@RequestBody TSjxxb reqDTO) {
        if(null==reqDTO.getBState()){
            reqDTO.setBState(0);//为空视为新增
            reqDTO.setBNotReturnNum(0);
            if(null==reqDTO.getBLendNum()){
                reqDTO.setBLendNum(0);
            }
            if(null==reqDTO.getBPrice()){
                reqDTO.setBPrice("99999");
            }
            if(null==reqDTO.getBAuthor()){
                reqDTO.setBAuthor("不明");
            }
            if(null==reqDTO.getBNum()){
                reqDTO.setBNum(0);
            }
            if(null==reqDTO.getBName()){
                throw new ServiceException("书名不可为空");
            }
            if(null==reqDTO.getBType()){
                throw new ServiceException("书类型不可为空");
            }

        }
        sjxxbService.saveOrUpdate(reqDTO);
        return super.success();
    }

    /**
     * 分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @PostMapping("/paging")
    public ApiRest<IPage<TSjxxb>> paging(@RequestBody PagingReqDTO<TSjxxb> reqDTO) {
        //分页查询并转换
        IPage<TSjxxb> page = sjxxbService.paging(reqDTO);
        return super.success(page);
    }


    /**
     * 批量删除
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiRest edit(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        sjxxbService.removeByIds(reqDTO.getIds());
        return super.success();
    }


    @ApiOperation(value = "修改状态")
    @PostMapping("/state")
    public ApiRest state(@RequestBody BaseStateReqDTO reqDTO) {
        QueryWrapper<TSjxxb> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(TSjxxb::getId, reqDTO.getIds());
        TSjxxb record = new TSjxxb();
        record.setBState(reqDTO.getState());
        sjxxbService.update(record, wrapper);
        return super.success();
    }


}

