package com.pnkx.web.controller.life;

import com.pnkx.common.annotation.Log;
import com.pnkx.common.core.controller.BaseController;
import com.pnkx.common.core.domain.AjaxResult;
import com.pnkx.common.core.domain.model.LoginUser;
import com.pnkx.common.core.page.TableDataInfo;
import com.pnkx.common.enums.BusinessType;
import com.pnkx.common.utils.ExcelUtil;
import com.pnkx.common.utils.SecurityUtils;
import com.pnkx.common.utils.ServletUtils;
import com.pnkx.domain.po.PxToDo;
import com.pnkx.framework.web.service.TokenService;
import com.pnkx.service.IPxToDoService;
import org.springframework.web.bind.annotation.*;

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

/**
 * 待办事项Controller
 *
 * @author phy
 * @date 2021-04-13
 */
@RestController
@RequestMapping("/admin/toDo")
public class PxToDoController extends BaseController {
    @Resource
    private IPxToDoService pxToDoService;
    @Resource
    private TokenService tokenService;

    /**
     * 查询待办事项列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PxToDo pxToDo) {
        startPage();
        List<PxToDo> list = pxToDoService.selectPxToDoList(pxToDo);
        return getDataTable(list);
    }

    /**
     * 导出待办事项列表
     */
    @Log(title = "待办事项", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PxToDo pxToDo) {
        List<PxToDo> list = pxToDoService.selectPxToDoList(pxToDo);
        ExcelUtil<PxToDo> util = new ExcelUtil<PxToDo>(PxToDo.class);
        return util.exportExcel(list, "do");
    }

    /**
     * 获取待办事项详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(pxToDoService.selectPxToDoById(id));
    }

    /**
     * 新增待办事项
     */
    @Log(title = "待办事项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PxToDo pxToDo) {
        pxToDo.setCreateBy(SecurityUtils.getUserId().toString());
        pxToDo.setPerformer(SecurityUtils.getUserId().toString());
        return toAjax(pxToDoService.insertPxToDo(pxToDo));
    }

    /**
     * 修改待办事项
     */
    @Log(title = "待办事项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PxToDo pxToDo) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        pxToDo.setUpdateBy(String.valueOf(loginUser.getUser().getUserId()));
        return toAjax(pxToDoService.updatePxToDo(pxToDo));
    }

    /**
     * 删除待办事项
     */
    @Log(title = "待办事项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pxToDoService.deletePxToDoByIds(ids));
    }

    /**
     * 获取待办事项标签列表
     */
    @GetMapping(value = "/getLabelList")
    public AjaxResult getLabelList() {
        return AjaxResult.success(pxToDoService.getLabelList());
    }
}
