package com.ruoyi.interfaces.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.interfaces.core.page.Page;
import com.ruoyi.interfaces.core.page.PageParam;
import com.ruoyi.interfaces.core.page.PageUtils;
import com.ruoyi.interfaces.domain.PtServiceApply;
import com.ruoyi.interfaces.service.PtServiceApplyService;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author weking
 * @date 2019-4-25
 */
@RestController
@RequestMapping("/pt/service/apply")
public class PtServiceApplyController extends BaseController {

    private final PtServiceApplyService ptServiceApplyService;

    @Autowired
    public PtServiceApplyController(PtServiceApplyService ptServiceApplyService) {
        this.ptServiceApplyService = ptServiceApplyService;
    }

    @ApiOperation("添加数据")
    @RequestMapping(value = "/request", method = RequestMethod.POST)
    public AjaxResult request(@RequestBody PtServiceApply ptServiceApply) {
        ptServiceApply.setApproveUser(String.valueOf(getUserId()));
        ptServiceApply.setUnitName(SecurityUtils.getUsername());
        int ret = ptServiceApplyService.insert(ptServiceApply);
        return ret > 0 ? AjaxResult.success() : AjaxResult.error();
    }

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

    @ApiOperation("根据主键获取数据")
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id") String id) {
        return AjaxResult.success(ptServiceApplyService.get(id));
    }

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

    @ApiOperation("数据列表")
    @PostMapping(value = "/list")
    public Page list(@RequestBody PageParam<PtServiceApply> ptServiceApply) {
        PtServiceApply serviceApply = Optional.ofNullable(ptServiceApply.getData()).orElse(new PtServiceApply());
        serviceApply.setApproveUser(String.valueOf(getUserId()));
        PageHelper.startPage(ptServiceApply);
        // PageHelper.orderBy("CATE_ID DESC");
        List<PtServiceApply> queryResult = ptServiceApplyService.list(serviceApply);
        return PageUtils.convert(ptServiceApplyService.convertPtServiceApply(queryResult));
    }

    @ApiOperation("数据列表")
    @PostMapping(value = "/user/list")
    public Page userList(@RequestBody PageParam<PtServiceApply> pageParam) {
        return ptServiceApplyService.listOfPage(pageParam);
    }

    @ApiOperation("数据列表")
    @PostMapping(value = "/listAll")
    public Page listAll(@RequestBody PageParam<PtServiceApply> ptServiceApply) {
        PageHelper.startPage(ptServiceApply);
        List<PtServiceApply> queryResult = ptServiceApplyService.list(ptServiceApply.getData());
        return PageUtils.convert(new PageInfo<>(queryResult));
    }
}
