package com.bootdo.datas.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.datas.domain.GyUnitDO;
import com.bootdo.datas.domain.UnitDO;
import com.bootdo.datas.dto.GyDataImportDTO;

import com.bootdo.datas.service.UnitService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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.List;
import java.util.Map;

/**
 * 数据处理者名称表
 * 
 * @author admin
 * @email admin@163.com
 * @date 2022-03-06 10:38:05
 */

@Controller
@RequestMapping("/datas/unit")
public class UnitController {
    @Autowired
    private UnitService unitService;


    @GetMapping()
    @Log("访问数据处理者名称")
    @RequiresPermissions("datas:unit:unit")
    String Unit() {
        return "datas/unit/unit";
    }

    @GetMapping("/dataList/{id}")
    @Log("访问数据处理者名称数据列表")
    @RequiresPermissions("datas:unit:dataList")
    String dataList(@PathVariable("id") Long id, Model model) {
        GyDataImportDTO unit = unitService.get(id);
        model.addAttribute("unit", unit);
        return "datas/unit/dataList";
    }


    @Log("访问数据处理者名称")
    @GetMapping("/unitInfo")
    @RequiresPermissions("datas:unit:unit")
    String unitInfo() {
        return "datas/unit/unitInfo";
    }

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

    @GetMapping("/add")
    @RequiresPermissions("datas:unit:add")
    String add() {
        return "datas/unit/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("datas:unit:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        GyUnitDO unit = unitService.getUnitById(id);
        model.addAttribute("unit", unit);
        return "datas/unit/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("datas:unit:add")
    public R save(UnitDO unit) {
        if (unitService.save(unit) > 0) {
            return R.ok();
        }
        return R.error();
    }


    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("datas:unit:remove")
    public R remove(Long id) {

        Long[] ids = new Long[]{id};
        int i = unitService.batchRemove(ids);
        if(i==0){
            return R.error("选中的责任主体中包含数据目录，不允许删除");
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("datas:unit:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {

        int i = unitService.batchRemove(ids);
        if(i==0){
            return R.error("选中的责任主体中包含数据目录，不允许删除");
        }
        return R.ok();
    }

}
