package com.bootdo.datas.controller;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.R;
import com.bootdo.datas.domain.DataPartDO;
import com.bootdo.datas.service.DataPartService;
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-27 23:33:17
 */

@Controller
@RequestMapping("/datas/dataPart")
public class DataPartController {

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

    @Autowired
    private DataPartService dataPartService;

    @Log("访问分类参考")
    @GetMapping()
    @RequiresPermissions("data:dataPart:dataPart")
    String DataPart() {
        return "datas/dataPart/dataPart";
    }

    @Log("访问分类参考")
    @GetMapping("dataPartView")
    @RequiresPermissions("data:dataPart:dataPart")
    String DataPartView() {
        return "datas/dataPart/dataPartView";
    }

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


    @Log("添加分类数据")
    @RequiresPermissions("data:dataPart:add")
    @GetMapping("/add/{pId}")
    String add(Model model, @PathVariable("pId") Long pId) {
        model.addAttribute("pId", pId);
        if (pId == 0) {
            model.addAttribute("pName", "根数据");
        } else {
            model.addAttribute("pName", dataPartService.get(pId).getPartName());
        }
        return "datas/dataPart/add";
    }

    @Log("编辑分类数据")
    @RequiresPermissions("data:dataPart:edit")
    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable("id") Long id) {
        DataPartDO dataPart = dataPartService.get(id);
        Long pId = dataPart.getParentId();
        model.addAttribute("pId", pId);
        if (pId == 0) {
            model.addAttribute("pName", "根数据");
        } else {
            model.addAttribute("pName", dataPartService.get(pId).getPartName());
        }
        log.info("dataPart:" + dataPart.toString());
        model.addAttribute("dataPart", dataPart);
        return "datas/dataPart/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("data:dataPart:add")
    public R save(DataPartDO dataPart) {
        dataPart.setDelFlag(0);
        if (dataPartService.save(dataPart) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("data:dataPart:edit")
    public R update(DataPartDO dataPart) {
        dataPartService.update(dataPart);
        return R.ok();
    }

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

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

    
    /**
     * @Description: 数据分类查询
     * @Author: wangp
     * @Date: 2022/2/28 13:06
     * @param partId
     * @param parentId
     * @Return: List<DataPartDO>
     */
    @ResponseBody
    @GetMapping("/list/{partId}/{parentId}")
    public List<DataPartDO> listByTypeParentID(@PathVariable("partId") String partId, @PathVariable("parentId") String parentId) {
        // 查询列表数据
        Map<String, Object> map = new HashMap<>(16);
        map.put("partId", partId);
        map.put("parentId", parentId);
        List<DataPartDO> list = dataPartService.list(map);
        return list;
    }

    /**
     * @Description: 数据分类查询
     * @Author: wangp
     * @Date: 2022/2/28 13:06
     * @param parentId
     * @Return: List<DataPartDO>
     */
    @ResponseBody
    @GetMapping("/list/{parentId}")
    public List<DataPartDO> listParentID(@PathVariable("parentId") String parentId) {
        // 查询列表数据
        Map<String, Object> map = new HashMap<>(16);
        map.put("parentId", parentId);
        List<DataPartDO> list = dataPartService.list(map);
        return list;
    }

}
