package im.zhaojun.system.controller;

import com.github.pagehelper.PageInfo;
import im.zhaojun.common.annotation.OperationLog;
import im.zhaojun.common.util.PageResultBean;
import im.zhaojun.common.util.ResultBean;
import im.zhaojun.system.model.Code;
import im.zhaojun.system.service.CodeService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/code")
public class CodeController {


    @Resource
    private CodeService codeService;

    @OperationLog("获取code列表")
    @GetMapping("/list")
    @ResponseBody
    public PageResultBean<Code> getList(@RequestParam(value = "page", defaultValue = "1") int page,
                                        @RequestParam(value = "limit", defaultValue = "10") int limit) {
        List<Code> codes = codeService.selectAllWithKey(page, limit);
        PageInfo<Code> userPageInfo = new PageInfo<>(codes);
        return new PageResultBean<>(userPageInfo.getTotal(), userPageInfo.getList());
    }

    @OperationLog("获取指定type的code列表")
    @GetMapping("/list/{typeid}")
    @ResponseBody
    public ResultBean getList(@PathVariable("typeid") String typeid) {
        List<Code> codes = codeService.selectAllWithType(typeid);
        return ResultBean.success(codes);
    };


    @OperationLog("更新code")
    @PostMapping("/update")
    public ResultBean update(@RequestBody List<Code> codelist) {

        int result = codeService.updatecode(codelist);
        return ResultBean.success();
    }


    @OperationLog("更新用户列表")
    @GetMapping("/codetest")
    public void test() {
        List<Code> codes = codeService.selectAllWithKey(1, 10);

    }

    @OperationLog("更新code time")
    @PostMapping("/updatetime")
    public ResultBean updatetime(@RequestBody List<Code> codelist) {

        int result = codeService.updatetime(codelist);
        return ResultBean.success();
    }

    @OperationLog("禁用账号")
    @PostMapping("/{codeId}/disable")
    @ResponseBody
    public ResultBean disable(@PathVariable("codeId") String codeId) {
        return ResultBean.success(codeService.disableCodeByCodeID(codeId));
    }

    @OperationLog("激活账号")
    @PostMapping("/{codeId}/enable")
    @ResponseBody
    public ResultBean enable(@PathVariable("codeId") String codeId) {
        return ResultBean.success(codeService.enableCodeByCodeID(codeId));
    }

    @GetMapping("/update/{name}/{alarmlevel}")
    public ResultBean update(@PathVariable("name") String name, @PathVariable("alarmlevel") String alarmlevel) {
      //更新手动状态
       return ResultBean.success(codeService.updatemanualcode(name,alarmlevel));
    }
}