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.PtApproval;
import com.ruoyi.interfaces.mapper.PtApprovalDao;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * @author weking
 * @date 2019-8-16
 */
@RestController
@RequestMapping("/pt/approval")
public class PtApprovalController extends BaseController {

    private final PtApprovalDao ptApprovalService;

    @Autowired
    public PtApprovalController(PtApprovalDao ptApprovalService) {
        this.ptApprovalService = ptApprovalService;
    }

    @ApiOperation("添加数据")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxResult insert(PtApproval ptApproval) {
        String uuid = IdUtils.simpleUUID();
        ptApproval.setId(uuid);
        ptApproval.setTm(new Date());
//        UserProfile userProfile = (UserProfile) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        ptApproval.setApprovalerId(userProfile.getUserId());
        int ret = ptApprovalService.insert(ptApproval);
        return ret > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @ApiOperation("数据列表")
    @RequestMapping(value = "/list")
    public Page list(int page, int rows, PtApproval ptApproval) {
        PageHelper.startPage(page, rows);
        // PageHelper.orderBy("CATE_ID desc");
        List<PtApproval> queryResult = ptApprovalService.selectByPrimaryKey(ptApproval);

        // 取分页信息
        PageInfo<PtApproval> pageInfo = new PageInfo<>(queryResult);
        return PageUtils.convert(pageInfo);
    }


    @ApiOperation("根据主键获取数据")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public AjaxResult get(PtApproval ptApproval) {
        List<PtApproval> ptApprovalList = ptApprovalService.selectByPrimaryKey(ptApproval);
        if (ptApprovalList.get(0) == null) {
            return AjaxResult.error("未找到相关数据！");
        } else {
            return AjaxResult.success(ptApprovalList.get(0));
        }
    }


    @ApiOperation("去编辑页面")
    @RequestMapping(value = "/to_edit")
    public ModelAndView toEdit(PtApproval ptApproval) {
        List<PtApproval> ptApprovalList = ptApprovalService.selectBySrvId(ptApproval);
        ModelAndView modelAndView = new ModelAndView();

        if (ptApprovalList.size() > 0) {
            modelAndView.addObject("ptApproval", ptApprovalList.get(0));
        }
        modelAndView.setViewName("pms/approval_view");
        return modelAndView;
    }


    @ApiOperation("根据主键更新数据")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public AjaxResult update(PtApproval ptApproval) {
        int ret = ptApprovalService.updateByPrimaryKey(ptApproval);
        return ret > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @ApiOperation("根据主键删除数据")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public AjaxResult delete(String keys) {
        String[] idArr = keys.split(",");
        for (String idStr : idArr) {
            ptApprovalService.deleteByPrimaryKey(idStr);
        }
        return AjaxResult.success();
    }

}
