package com.ruoyi.interfaces.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.PtAppApply;
import com.ruoyi.interfaces.service.PtAppApplyService;
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 2018-11-6
 */
@RestController
@RequestMapping("/pt/app/apply")
public class PtAppApplyController extends BaseController {

    private final PtAppApplyService ptAppApplyService;

    @Autowired
    public PtAppApplyController(PtAppApplyService ptAppApplyService) {
        this.ptAppApplyService = ptAppApplyService;
    }

    @ApiOperation("添加数据")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxResult insert(PtAppApply ptAppApply) {
        ptAppApply.setUserId("用户id");
        ptAppApply.setRqTime(new Date());

        int ret = ptAppApplyService.insert(ptAppApply);
        return ret > 0 ? AjaxResult.success() : AjaxResult.error();
    }

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

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


    @ApiOperation("根据主键获取数据")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public AjaxResult get(PtAppApply ptAppApply) {
        List<PtAppApply> ptAppApplyList = ptAppApplyService.selectByPrimaryKey(ptAppApply);

        if (ptAppApplyList.get(0) == null) {
            return AjaxResult.error("未找到相关数据！");
        } else {
            return AjaxResult.success(ptAppApplyList.get(0));
        }
    }


    @ApiOperation("去编辑页面")
    @RequestMapping(value = "/to_edit")
    public ModelAndView toEdit(PtAppApply ptAppApply) {
        List<PtAppApply> paaList = ptAppApplyService.selectByPrimaryKey(ptAppApply);

        ModelAndView modelAndView = new ModelAndView();
        if (paaList.size() > 0) {
            modelAndView.addObject("ptAppApply", paaList.get(0));
        }
        modelAndView.setViewName("mng/pt_app_apply_edit");

        return modelAndView;
    }

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

    @ApiOperation("根据主键删除数据")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public AjaxResult delete(String keys, String appIds) {
        String[] idArr = keys.split(",");
        String[] appIdArr = appIds.split(",");
        for (int i = 0; i < idArr.length; i++) {
            PtAppApply ptAppApply = new PtAppApply();
            String idStr = idArr[i];
            String appIdStr = appIdArr[i];
            ptAppApply.setUserId(idStr);
            ptAppApply.setAppId(appIdStr);
            ptAppApplyService.deleteByPrimaryKey(ptAppApply);
        }
        return AjaxResult.success();
    }

}
