package com.bootdo.data.controller;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.*;
import com.bootdo.data.domain.DataApprovalDO;
import com.bootdo.data.domain.UnitApplyInfoDO;
import com.bootdo.data.service.DataApprovalService;
import com.bootdo.data.service.UnitInfoService;
import com.bootdo.system.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 审批
 * @Author: wangp
 * @Date: 2022/3/2 14:06
 */
@Controller
@RequestMapping("/data/dataApproval")
public class DataApprovalController {

    private static Logger log = LoggerFactory.getLogger(UnitInfoController.class);

    @Autowired
    private UnitInfoService unitInfoService;

    @Autowired
    private DataApprovalService dataApprovalService;

    @Log("访问备案审核")
    @GetMapping()
    @RequiresPermissions("data:dataApproval:dataApproval")
    String DataApproval() {
        return "data/dataApproval/dataApproval";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("data:dataApproval:dataApproval")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<UnitApplyInfoDO> dataApprovalList = dataApprovalService.list(query);
        int total = dataApprovalService.count(query);
        PageUtils pageUtils = new PageUtils(dataApprovalList, total);
        return pageUtils;
    }

    /**
     * @Description: 备案详情
     * @Author: wangp
     * @Date: 2022/3/2 10:34
     * @param param
     * @param model
     * @Return: String
     */
    @GetMapping("/edit/{param}")
    @RequiresPermissions("data:unitInfo:unitInfo")
    String edit(@PathVariable("param") String param, Model model) {
        log.info("param:" + param);
        UserDO currUser = ShiroUtils.getUser();
        // 查询列表数据
        Map<String, Object> map = new HashMap<>(16);
        map.put("uploadUserid", currUser.getUserId());
        map.put("id", param);
        List<UnitApplyInfoDO> list = unitInfoService.listEditInfo(map);
        UnitApplyInfoDO unitInfo = new UnitApplyInfoDO();
        if (list != null && list.size() > 0) {
            // 已维护过
            unitInfo = list.get(0);
            // model.addAttribute("unitInfo", unitInfo);
            // return "data/unitInfo/edit";
        }
        model.addAttribute("unitInfo", unitInfo);
        log.info("param:" + param);
        model.addAttribute("paramInfo", param);
        model.addAttribute("pName", "根数据");
        return "/data/dataApproval/verifyInfo";
    }

    /**
     * @Description: 详情页面 审核通过 /驳回
     * @Author: wangp
     * @Date: 2022/3/2 15:00
     * @param dataApproval
     * @Return: R
     */
    @ResponseBody
    @PostMapping("/verifyInfo")
    @RequiresPermissions("data:dataApproval:verify")
    public R verifyInfo(DataApprovalDO dataApproval) {
        log.info("dataApproval:" + dataApproval.toString());
        UserDO currUser = ShiroUtils.getUser();
        log.info("currUser:" + currUser.toString());
        if (null == currUser) {
            dataApproval.setApprovalUserid("-1");
            dataApproval.setApprovalUser("获取用户信息为空");
        } else {
            dataApproval.setApprovalUserid(String.valueOf(ShiroUtils.getUserId()));
            dataApproval.setApprovalUser(ShiroUtils.getUser().getUsername());
        }
        if (dataApprovalService.verifyInfo(dataApproval) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("data:dataApproval:edit")
    public R update(DataApprovalDO dataApproval) {
        dataApprovalService.update(dataApproval);
        return R.ok();
    }

    /**
     * @Description: 列表页面 审核通过
     * @Author: wangp
     * @Date: 2022/3/3 10:11
     * @param applyId
     * @Return: R
     */
    @PostMapping("/verify")
    @ResponseBody
    @RequiresPermissions("data:dataApproval:verify")
    public R verify(Long applyId) {
        log.info("审核通过 备案主键 applyId:" + applyId);
        String status = "0";// dataApproval.getStatus() 审批状态 0:通过 1:驳回 3:取消
        String desc = "审核同意";
        if (dataApprovalService.approval(applyId, status, desc)) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * @Description: 批量审核
     * @Author: wangp
     * @Date: 2022/3/3 14:10
     * @param ids
     * @Return: R
     */
    @PostMapping("/batchVerify")
    @ResponseBody
    @RequiresPermissions("data:dataApproval:verify")
    public R batchVerify(@RequestParam("ids[]") Long[] ids) {
        log.info("审核通过 备案主键 ids:" + ids.toString());
        String status = "0";// dataApproval.getStatus() 审批状态 0:通过 1:驳回 3:取消
        String desc = "审核同意";
        // 审批数据遍历
        for (Long applyId : ids) {
            log.info("审核通过 备案主键 applyId:" + applyId);
            // 查询数组是否需要审核
            Map<String, Object> params = new HashMap<>();
            params.put("applyId", applyId);
            List<UnitApplyInfoDO> list = dataApprovalService.viewList(params);
            UnitApplyInfoDO unitApplyInfoDO = null;
            if (list != null && list.size() > 0) {
                unitApplyInfoDO = list.get(0);
            }
            log.info("staus" + unitApplyInfoDO.getApprovalStatus());
            // staus为空处于待审核状态 // dataApproval.getStatus() 审批状态 0:通过 1:驳回 3:取消
            if (StringUtils.isBlank(unitApplyInfoDO.getApprovalStatus())) {
                log.info("备案主键 applyId:" + applyId + " ,状态staus:" + unitApplyInfoDO.getApprovalStatus());
                dataApprovalService.approval(applyId, status, desc);
            } else {
                log.info("备案主键 applyId:" + applyId + " 无需审核");
            }
        }
        return R.ok();
    }

    /**
     * @Description: 列表页面 驳回
     * @Author: wangp
     * @Date: 2022/3/3 10:11
     * @param applyId
     * @Return: R
     */
    @PostMapping("/rebut")
    @ResponseBody
    @RequiresPermissions("data:dataApproval:rebut")
    public R rebut(Long applyId) {
        log.info("驳回 备案主键 applyId:" + applyId);
        String status = "1";// dataApproval.getStatus() 审批状态 0:通过 1:驳回 3:取消
        String desc = "驳回";
        if (dataApprovalService.approval(applyId, status, desc)) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * @Description: 批量审核
     * @Author: wangp
     * @Date: 2022/3/3 14:10
     * @param ids
     * @Return: R
     */
    @PostMapping("/batchRebut")
    @ResponseBody
    @RequiresPermissions("data:dataApproval:rebut")
    public R batchRebut(@RequestParam("ids[]") Long[] ids) {
        log.info("审核通过 备案主键 ids:" + ids);
        String status = "1";// dataApproval.getStatus() 审批状态 0:通过 1:驳回 3:取消
        String desc = "驳回";
        for (Long applyId : ids) {
            log.info("审核通过 备案主键 applyId:" + applyId);
            // 查询数组是否需要审核
            Map<String, Object> params = new HashMap<>();
            params.put("applyId", applyId);
            List<UnitApplyInfoDO> list = dataApprovalService.viewList(params);
            UnitApplyInfoDO unitApplyInfoDO = null;
            if (list != null && list.size() > 0) {
                unitApplyInfoDO = list.get(0);
            }
            log.info("staus" + unitApplyInfoDO.getApprovalStatus());
            // staus为空处于待审核状态 // dataApproval.getStatus() 审批状态 0:通过 1:驳回 3:取消
            if (StringUtils.isBlank(unitApplyInfoDO.getApprovalStatus())) {
                log.info("备案主键 applyId:" + applyId + " ,状态staus:" + unitApplyInfoDO.getApprovalStatus());
                dataApprovalService.approval(applyId, status, desc);
            } else {
                log.info("备案主键 applyId:" + applyId + " 无需审核");
            }
        }
        return R.ok();
    }

    /**
     * @Description: 列表页面 取消
     * @Author: wangp
     * @Date: 2022/3/3 10:11
     * @param applyId
     * @Return: R
     */
    @PostMapping("/cancel")
    @ResponseBody
    @RequiresPermissions("data:dataApproval:rebut")
    public R cancel(Long applyId) {
        log.info("取消 备案主键 applyId:" + applyId);
        String status = "3";// dataApproval.getStatus() 审批状态 0:通过 1:驳回 3:取消
        String desc = "";
        if (dataApprovalService.approval(applyId, status, desc)) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("data:dataApproval:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {
        dataApprovalService.batchRemove(ids);
        return R.ok();
    }

    @GetMapping("/view/{applyId}")
    @RequiresPermissions("data:dataApproval:view")
    String view(@PathVariable("applyId") String applyId, Model model) {
        log.info("applyId:" + applyId);
        UserDO currUser = ShiroUtils.getUser();
        // 查询列表数据
        Map<String, Object> params = new HashMap<>();
        params.put("applyId", applyId);
        List<UnitApplyInfoDO> list = dataApprovalService.viewList(params);
        UnitApplyInfoDO unitApplyInfoDO = null;
        if (list != null && list.size() > 0) {
            // 已维护过
            unitApplyInfoDO = list.get(0);
            // model.addAttribute("unitInfo", unitInfo);
            // return "data/unitInfo/edit";
        }
        log.info("unitApplyInfoDO:" + unitApplyInfoDO.toString());
        model.addAttribute("unitInfo", unitApplyInfoDO);
        log.info("param:" + applyId);
        model.addAttribute("paramInfo", applyId);
        return "/data/dataApproval/viewInfo";
    }

}
