package com.xhkjedu.sapi.controller.system;

import com.xhkjedu.annotation.Action;
import com.xhkjedu.sapi.model.system.TUserModule;
import com.xhkjedu.sapi.service.system.UserModuleService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.sapi.vo.system.UserModuleVo;
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 UserModuleController
 * Description 用户常用应用
 * Author WN
 * Date 2020/6/15 15:06
 **/
@RestController
@RequestMapping("/umodule")
public class UserModuleController {

    @Autowired
    private UserModuleService userModuleService;

    //用户常用应用集合
    @PostMapping("/list")
    public ResultVo listModules(@RequestBody TUserModule userModule){
        N_Utils.validation(new Object[]{userModule.getUserid(),"用户id",1});
        List<UserModuleVo> lst = userModuleService.listUserModules(userModule.getUserid(),userModule.getBelongcode());
        return new ResultVo(0,"获取用户常用应用成功",lst);
    }


    /**
     *功能描述 保存
     * @author WN
     * @date 2020/6/11
     * @param  * @param module
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/save")
    @Action("常用应用-增加")
    public ResultVo save(@RequestBody TUserModule userModule){
        N_Utils.validation(new Object[]{
                userModule.getUserid(),"用户id",1,
                userModule.getSchoolid(),"学校id",1
        });
        /*if(userModule.getModuleids()==null || userModule.getModuleids().size()==0){
            return new ResultVo(1,"请选择常用应用");
        }else{

        }*/
        int result = 0;
        try {
            result = userModuleService.save(userModule);
        } catch (Exception e) {
            return new ResultVo(1, "保存失败");
        }
        if (result > 0) {
            return new ResultVo(0, "保存成功");
        }else{
            return new ResultVo(1, "保存失败");
        }
    }

    /**
     *功能描述 修改排序
     * @author WN
     * @date 2020/6/11
     * @param  * @param module
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/up")
    @Action("常用应用-修改")
    public ResultVo update(@RequestBody TUserModule userModule){
        N_Utils.validation(new Object[]{
                userModule.getUmid(),"用户应用id",1,
                userModule.getUmorder(),"应用显示顺序",1
        });
        int result = userModuleService.updateOrder(userModule);
        if (result > 0) return new ResultVo(0,"修改成功");
        else return new ResultVo(1,"修改失败");
    }

    /**
     *功能描述  删除
     * @author WN
     * @date 2020/6/11
     * @param  * @param module
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/del")
    @Action("常用应用-删除")
    public ResultVo del(@RequestBody TUserModule userModule) {
        if (!N_Utils.isTrueInteger(userModule.getUmid()))
            return new ResultVo(1,"未选择删除的常用应用");
        int result = userModuleService.del(userModule.getUmid());
        if (result > 0) return new ResultVo(0,"删除成功");
        else return new ResultVo(1,"删失败");
    }
}
