package com.elephant.framework.galaxy.aroad.module.zhjg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.elephant.framework.galaxy.aroad.module.common.annotation.SysLog;
import com.elephant.framework.galaxy.aroad.module.common.controller.BaseController;
import com.elephant.framework.galaxy.aroad.module.zhjc.entity.ZhjcGhjh;
import com.elephant.framework.galaxy.aroad.module.zhjg.entity.ZhjgDckhRw;
import com.elephant.framework.galaxy.aroad.module.zhjg.entity.ZhjgDckhRwFf;
import com.elephant.framework.galaxy.aroad.module.zhjg.service.ZhjgDckhRwFfService;
import com.elephant.framework.galaxy.aroad.module.zhjg.service.ZhjgDckhRwService;
import com.elephant.framework.galaxy.aroad.response.R;
import com.elephant.framework.galaxy.aroad.security.entity.GalaxyUser;
import com.elephant.framework.galaxy.aroad.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 督查情况任务表
 *
 * @author zyw
 * @date 2019-03-15 16:58:24
 */
@RestController
@AllArgsConstructor
@RequestMapping("/zhjg/zhjgdckhrw")
public class ZhjgDckhRwController extends BaseController {

    private final ZhjgDckhRwService zhjgDckhRwService;
    private final ZhjgDckhRwFfService zhjgDckhRwFfService;
    private static Integer loginDeptId = null;

    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param zhjgDckhRw 督查情况任务表
     * @return
     */
    @GetMapping("/page")
    public R getZhjgDckhRwPage(Page page, ZhjgDckhRw zhjgDckhRw) {
        Page<ZhjgDckhRw> iPage = new Page<ZhjgDckhRw>();
        //是否具备创建任务的权限
        boolean isCreatRw = SecurityUtils.isMenue("zhjg_dckhdctg_add");
        if (isCreatRw) {
            QueryWrapper<ZhjgDckhRw> queryWrapper = new QueryWrapper<>();
            if (zhjgDckhRw.getRwmc() != null) {
                queryWrapper.like("rwmc", zhjgDckhRw.getRwmc());
            }
            if (zhjgDckhRw.getZrdwid() != null) {
                queryWrapper.eq("zrdwid", zhjgDckhRw.getZrdwid());
            }
              List<ZhjgDckhRw> list = zhjgDckhRwService.list(queryWrapper);
              for (ZhjgDckhRw rw :list){
                  rw.setCydws(rw.getCydw().split(","));
              }

              iPage.setRecords(list);
              iPage.setCurrent(page.getSize());
              iPage.setTotal(list.size());
              iPage.setSize(page.getSize());

         } else {

           // iPage = zhjgDckhRwService.selectRwForPage(page, zhjgDckhRw, loginDeptId);
         }


        return new R<>(iPage);
    }

    /**
     * 待办分页查询
     *
     * @param page       分页对象
     * @param zhjgDckhRw 督查情况任务表
     * @return
     */
    @GetMapping("/db/page")
    public R getZhjgDckhRwDbPage(Page page, ZhjgDckhRw zhjgDckhRw) {
     GalaxyUser galaxyUser = SecurityUtils.getUser();
//    IloginDeptId = galaxyUser.getDeptId();
//    zhjgDckhRw.setCydw(String.valueOf(deptId));
        zhjgDckhRw.setRwzt("1");
        return new R<>(zhjgDckhRwService.selectRwForPage(page, zhjgDckhRw, loginDeptId));
    }

    /**
     * 已办分页查询
     *
     * @param page       分页对象
     * @param zhjgDckhRw 督查情况任务表
     * @return
     */
    @GetMapping("/yb/page")
    public R getZhjgDckhRwYbPage(Page page, ZhjgDckhRw zhjgDckhRw) {
//    GalaxyUser galaxyUser = SecurityUtils.getUser();
//    Integer deptId = galaxyUser.getDeptId();
//    zhjgDckhRw.setCydw(String.valueOf(deptId));
        zhjgDckhRw.setStatus("4");
        return new R<>(zhjgDckhRwService.selectRwForPage(page, zhjgDckhRw, loginDeptId));
    }


    /**
     * 通过id查询督查情况任务表
     *
     * @param id id
     * @return R
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return new R<>(zhjgDckhRwService.getById(id));
    }

    /**
     * 新增督查情况任务表
     *
     * @param zhjgDckhRw 督查情况任务表
     * @return R
     */
    @SysLog("新增督查情况任务表")
    @PostMapping
    public R save(@RequestBody ZhjgDckhRw zhjgDckhRw) {
        zhjgDckhRw.setFkzt("0");
        boolean rw = zhjgDckhRwService.save(zhjgDckhRw);
        return new R<>(rw);
    }

    /**
     * 修改督查情况任务表
     *
     * @param zhjgDckhRw 督查情况任务表
     * @return R
     */
    @SysLog("修改督查情况任务表")
    @PutMapping
    public R updateById(@RequestBody ZhjgDckhRw zhjgDckhRw) {
        return new R<>(zhjgDckhRwService.updateById(zhjgDckhRw));
    }

    /**
     * 通过id删除督查情况任务表
     *
     * @param id id
     * @return R
     */
    @SysLog("删除督查情况任务表")
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable Integer id) {
        return new R<>(zhjgDckhRwService.removeById(id));
    }

    /**
     * 通过id下发任务
     *
     * @param id id
     * @return R
     */
    @SysLog("督查任务下发")
    @PutMapping("/download/{id}")
    public R downloadById(@PathVariable Integer id) {
        return new R<>(zhjgDckhRwService.downloadById(id));
    }
}
