package com.bootdo.data.controller;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.service.DictService;
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.DataInfoDO;
import com.bootdo.data.service.DataInfoService;
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.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 重要数据表
 * 
 * @author admin
 * @email admin@163.com
 * @date 2022-02-27 17:27:36
 */

@Controller
@RequestMapping("/data/dataInfo")
public class DataInfoController {

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

    @Autowired
    private DataInfoService dataInfoService;

    @Autowired
    private DictService dictService;

    @Log("访问数据填报")
    @GetMapping()
    @RequiresPermissions("data:dataInfo:dataInfo")
    String DataInfo() {
        return "data/dataInfo/dataInfo";
    }

    /**
     * @Description: 上报数据整理
     * @Author: wangp
     * @Date: 2022/3/1 0:27
     * @param params
     * @Return: PageUtils
     */
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("data:dataInfo:dataInfo")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<DataInfoDO> dataInfoList = dataInfoService.list(query);
        int total = dataInfoService.count(query);
        PageUtils pageUtils = new PageUtils(dataInfoList, total);
        return pageUtils;
    }

    @ResponseBody
    @GetMapping("/Querylist")
    @RequiresPermissions("data:dataPart:dataPart")
    public List<DataInfoDO> Querylist(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        List<DataInfoDO> dataPartList = dataInfoService.list(params);
        return dataPartList;
    }

    /**
     * @Description:重要数据和核心数据目录备案清单 初始化
     * @Author: wangp
     * @Date: 2022/3/1 19:05
     * @param params
     * @Return: List<DataInfoDO>
     */
    @ResponseBody
    @GetMapping("/Querylist/{param}")
    @RequiresPermissions("data:dataPart:dataPart")
    public List<DataInfoDO> querylistInfo(@PathVariable("param") String param,
        @RequestParam Map<String, Object> params) {
        log.info("param:" + param);
        // 查询列表数据
        List<DataInfoDO> dataPartList = new ArrayList<>();
        if (!"flag".equals(param)) {
            UserDO currUser = ShiroUtils.getUser();
            params.put("uploadUserid", currUser.getUserId());
            dataPartList = dataInfoService.querylistInfo(params);
        }
        return dataPartList;
    }

    @GetMapping("/add")
    @RequiresPermissions("data:dataInfo:add")
    String add() {
        return "data/dataInfo/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("data:dataInfo:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        DataInfoDO dataInfo = dataInfoService.get(id);
        model.addAttribute("dataInfo", dataInfo);
        // model.addAttribute("dataTypes", dictDOS);
        return "data/dataInfo/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("data:dataInfo:add")
    public R save(DataInfoDO dataInfo) {
        UserDO currUser = ShiroUtils.getUser();
        log.info("currUser:" + currUser.toString());
        if (null == currUser) {
            dataInfo.setUploadUserid("-1");
            dataInfo.setUploadUser("获取用户信息为空");
        } else {
            dataInfo.setUploadUserid(String.valueOf(ShiroUtils.getUserId()));
            dataInfo.setUploadUser(ShiroUtils.getUser().getUsername());
        }
        dataInfo.setDataStatus("0"); //状态 0:正常 1:删除 2:已上报 3:通过审核 4:驳回
        if (dataInfoService.save(dataInfo) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("data:dataInfo:edit")
    public R update(DataInfoDO dataInfo) {
        dataInfoService.update(dataInfo);
        return R.ok();
    }

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

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

}
