package com.ruoyi.interfaces.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.interfaces.core.page.Page;
import com.ruoyi.interfaces.core.page.PageUtils;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.interfaces.domain.PtModel;
import com.ruoyi.interfaces.mapper.PtModelMapper;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/pt/model")
public class PtModelController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(PtModelController.class);

    @Autowired
    private PtModelMapper ptModelMapper;

    @ApiOperation("添加模型")
    @PostMapping(value = "/add")
    public AjaxResult add(PtModel ptModel) {
        ptModel.setMid(IdUtils.simpleUUID());
        return ptModelMapper.insert(ptModel) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @ApiOperation("修改模型")
    @GetMapping(value = "/update")
    public AjaxResult update(PtModel ptModel) {
        return ptModelMapper.updateByPrimaryKey(ptModel) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @ApiOperation("修改模型")
    @GetMapping(value = "/status")
    public AjaxResult status(@RequestParam(required = true) String mid,
                               @RequestParam(required = true) String flag) {

        try {
            ptModelMapper.updateModelStatus(mid, flag);
            return AjaxResult.success();
        } catch (Exception e) {
            //在此处调用
            logger.error(e.getLocalizedMessage(), e);
            return AjaxResult.error();
        }
    }

    @ApiOperation("/删除模型")
    @RequestMapping(value = "/del")
    public AjaxResult del(String ids) {
        String[] strs = ids.split(",");
        int deletes = 0;
        for (String str : strs) {
            deletes += ptModelMapper.deleteByPrimaryKey(str);
        }
        return deletes == strs.length ? AjaxResult.success() : AjaxResult.error();
    }

    @ApiOperation("去编辑模型界面")
    @RequestMapping(value = "/to_edit")
    public ModelAndView toEdit(String mid) {
        ModelAndView modelAndView = new ModelAndView();
        PtModel ptModel = ptModelMapper.selectByPrimaryKey(mid);
        modelAndView.addObject("ptModel", ptModel);
        modelAndView.setViewName("mng/pt_model_edit");
        return modelAndView;
    }

    @ApiOperation("模型列表")
    @RequestMapping(value = "/list")
    public Page list(@RequestParam(required = false, defaultValue = "1") int page,
                     @RequestParam(required = false, defaultValue = "100") int rows, PtModel ptModel) {
        PageHelper.startPage(page, rows);
        List<PtModel> list = ptModelMapper.selectAll(ptModel);
        PageInfo<PtModel> pageInfo = new PageInfo<>(list);
        return PageUtils.convert(pageInfo);
    }
}
