package com.bootdo.data.controller;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.data.domain.UnitApplyInfoDO;
import com.bootdo.data.domain.UnitInfoDO;
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;

/**
 * 数据处理者名称表
 * 
 * @author admin
 * @email admin@163.com
 * @date 2022-02-28 17:08:50
 */

@Controller
@RequestMapping("/data/unitInfo")
public class UnitInfoController {

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

    @Autowired
    private UnitInfoService unitInfoService;

    @Log("访问备案上报")
    @GetMapping()
    @RequiresPermissions("data:unitInfo:unitInfo")
    String UnitInfo(Model model) {
        // UserDO currUser = ShiroUtils.getUser();
        //// 查询列表数据
        // Map<String, Object> map = new HashMap<>(16);
        // map.put("uploadUserid", currUser.getUserId());
        // List<UnitInfoDO> list = unitInfoService.list(map);
        // UnitInfoDO unitInfo = new UnitInfoDO();
        // if (list != null && list.size() > 0) {
        // // 已维护过
        // unitInfo = list.get(0);
        // // model.addAttribute("unitInfo", unitInfo);
        // // return "data/unitInfo/edit";
        // }
        // model.addAttribute("unitInfo", unitInfo);
        return "data/unitInfo/unitInfo";
    }

    /**
     * @Description: 申请备案
     * @Author: wangp
     * @Date: 2022/3/1 12:32
     * @param model
     * @Return: String
     */
    @GetMapping("/ask/{param}")
    @RequiresPermissions("data:unitInfo:unitInfo")
    String ask(@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());
        List<UnitInfoDO> list = unitInfoService.list(map);
        UnitInfoDO unitInfo = new UnitInfoDO();
        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/unitInfo/askInfo";
    }


    @GetMapping("/askEdit/{param}")
    @RequiresPermissions("data:unitInfo:unitInfo")
    String askEdit(@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/unitInfo/askEditInfo";
    }

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

    /**
     * @Description: 单位信息维护
     * @Author: wangp
     * @Date: 2022/2/28 17:26
     * @param
     * @Return: String
     */
    @GetMapping("/unitInfo")
    @RequiresPermissions("data:unitInfo:add")
    String Info(Model model) {
        UserDO currUser = ShiroUtils.getUser();
        String flag = "false";
        // 查询列表数据
        Map<String, Object> map = new HashMap<>(16);
        map.put("uploadUserid", currUser.getUserId());
        List<UnitInfoDO> list = unitInfoService.list(map);
        UnitInfoDO unitInfo = new UnitInfoDO();
        if (list != null && list.size() > 0) {
            // 已维护过
            unitInfo = list.get(0);
            flag = "true";
            // model.addAttribute("unitInfo", unitInfo);
            // return "data/unitInfo/edit";
        }
        model.addAttribute("unitInfo", unitInfo);
        model.addAttribute("flag", flag);
        return "data/unitInfo/edit";
    }

    /**
     * @Description:
     * @Author: wangp
     * @Date: 2022/3/1 0:29
     * @param
     * @Return: String
     */
    @GetMapping("/queryDataInfo")
    @RequiresPermissions("data:unitInfo:add")
    String queryDataInfo() {
        return "data/unitInfo/queryDataInfo";
    }

    /**
     * @Description: 备案申请
     * @Author: wangp
     * @Date: 2022/2/28 17:27
     * @param
     * @Return: String
     */
    @GetMapping("/add")
    @RequiresPermissions("data:unitInfo:add")
    String add() {
        return "data/unitInfo/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("data:unitInfo:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        UnitInfoDO unitInfo = unitInfoService.get(id);
        model.addAttribute("unitInfo", unitInfo);
        return "data/unitInfo/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("data:unitInfo:add")
    public R save(UnitInfoDO unitInfo) {

        UserDO currUser = ShiroUtils.getUser();
        log.info("currUser:" + currUser.toString());
        if (null == currUser) {
            unitInfo.setUploadUserid("-1");
            unitInfo.setUploadUser("获取用户信息为空");
        } else {
            unitInfo.setUploadUserid(String.valueOf(ShiroUtils.getUserId()));
            unitInfo.setUploadUser(ShiroUtils.getUser().getUsername());
        }
        unitInfo.setStatus("0");// 状态
        if (unitInfoService.save(unitInfo) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("data:unitInfo:edit")
    public R update(UnitInfoDO unitInfo) {
        unitInfoService.update(unitInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/askRemove")
    @ResponseBody
    @RequiresPermissions("data:unitInfo:remove")
    public R askRemove(Long id) {
        if (unitInfoService.askRemove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

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

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("data:unitInfo:remove")
    public R remove(Long id) {
        if (unitInfoService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

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

}
