package com.xhkjedu.sstudy.controller.subjectbook;

import com.xhkjedu.annotation.Action;
import com.xhkjedu.sstudy.model.subjectbook.TVersion;
import com.xhkjedu.sstudy.service.subjectbook.VersionService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import java.util.List;

/**
 * @ClassName VersionController
 * Description TODO  教材版本
 * Author WN
 * Date 2020/6/1 12:03
 **/
@RestController
@RequestMapping("/version")
public class VersionController {
    @Autowired
    private VersionService versionService;

    /*
     * @Description 版本管理列表
     * @Date 2023/11/28 10:30:32
     * @Author WN
     * @Param [version]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_manage")
    public ResultVo listManage(@RequestBody TVersion version){
        List<TVersion> lst = versionService.listVersions(version);
        return new ResultVo(0,"查询教材版本集合成功",lst);
    }

    /**
     *功能描述 列表
     * @author WN
     * @date 2020/6/2
     * @param  * @param version
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/list")
    public ResultVo listBooks(@RequestBody TVersion version){
        List<TVersion> lst = versionService.listVersions(version);
        return new ResultVo(0,"查询教材版本集合成功",lst);
    }

   /**
    *功能描述  添加
    * @author WN
    * @date 2020/6/2
    * @param  * @param version
    * @return com.xhkjedu.vo.ResultVo
    */
    @PostMapping("/add")
    @Action("版本管理-增加")
    public ResultVo add(@RequestBody TVersion version){
        N_Utils.validation(new Object[]{version.getVersionname(),"教材版本名称",2});
        int result = versionService.addVersion(version);
        if (result > 0) {
            return new ResultVo(0, "保存成功");
        }else{
            return new ResultVo(1, "保存失败");
        }
    }

    /**
     *功能描述  修改教材版本
     * @author WN
     * @date 2020/6/2
     * @param  * @param version
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/update")
    @Action("版本管理-修改")
    public ResultVo update(@RequestBody TVersion version){
        N_Utils.validation(new Object[]{version.getVersionid(),"教材版本id",2,version.getVersionname(),"教材版本名称",2});
        int result = versionService.updateVersion(version);
        if (result > 0) return new ResultVo(0,"修改成功");
        else return new ResultVo(1,"修改失败");
    }

    /**
     *功能描述 删除教材版本
     * @author WN
     * @date 2020/6/2
     * @param  * @param version
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/del")
    @Action("版本管理-删除")
    public ResultVo del(@RequestBody TVersion version) {
        if (N_Utils.isEmpty(version.getVersionid()))
            return new ResultVo(1,"未选择删除的教材版本");
        String useModule = versionService.findUseModule(version.getVersionid());
        if (N_Utils.isNotEmpty(useModule)) {
//            return new ResultVo(1,"使用版本模块【"+useModule+"】，禁止删除");
            return new ResultVo(1,"此版本下已有教材，禁止删除");
        }
        int result = versionService.delVersion(version);
        if (result > 0) return new ResultVo(0,"删除成功");
        else return new ResultVo(1,"删除失败");
    }


}
