package com.dcsoft.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dcsoft.common.log.annotation.Log;
import com.dcsoft.common.log.enums.BusinessType;
import com.dcsoft.common.security.annotation.RequiresPermissions;
import com.dcsoft.system.domain.SysRule;
import com.dcsoft.system.service.ISysRuleService;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.utils.poi.ExcelUtil;
import com.dcsoft.common.core.web.page.TableDataInfo;

/**
 * 通行规则Controller
 *
 * @author nichun
 * @date 2023-03-08
 */
@RestController
@RequestMapping("/rule")
public class SysRuleController extends BaseController
{
    @Autowired
    private ISysRuleService sysRuleService;

    /**
     * 查询通行规则列表
     */
    @RequiresPermissions("system:rule:list")
    @GetMapping("/list")
    public TableDataInfo list(SysRule sysRule)
    {
        startPage();
        List<SysRule> list = sysRuleService.selectSysRuleList(sysRule);
        return getDataTable(list);
    }

    @GetMapping("/lists")
    public AjaxResult lists(SysRule sysRule)
    {
        List<SysRule> list = sysRuleService.selectSysRuleList(sysRule);
        return AjaxResult.success(list);
    }

    /**
     * 导出通行规则列表
     */
    @RequiresPermissions("system:rule:export")
    @Log(title = "通行规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRule sysRule)
    {
        List<SysRule> list = sysRuleService.selectSysRuleList(sysRule);
        ExcelUtil<SysRule> util = new ExcelUtil<SysRule>(SysRule.class);
        util.exportExcel(response, list, "通行规则数据");
    }

    /**
     * 获取通行规则详细信息
     */
    @RequiresPermissions("system:rule:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        SysRule rule=sysRuleService.selectSysRuleById(id);
        if(StringUtils.isNotEmpty(rule.getPermission())){
            String[] checkList=rule.getPermission().split(",");
            rule.setCheckList(checkList);
        }
        return success(rule);
    }

    /**
     * 新增通行规则
     */
    @RequiresPermissions("system:rule:add")
    @Log(title = "通行规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRule rule)
    {
        String permission="";
        for(String s:rule.getCheckList()){
            permission+=s+",";
        }
        if(!"".equals(permission)){
            permission=permission.substring(0,permission.length()-1);
        }
        rule.setPermission(permission);
        if(rule.getPointIds()!=null){
            String pointIds="";
            for(String s:rule.getPointIds()){
                pointIds+=s+",";
            }
            if(!"".equals(pointIds)){
                pointIds=pointIds.substring(0,pointIds.length()-1);
            }
            rule.setPointId(pointIds);
        }
        return toAjax(sysRuleService.insertSysRule(rule));
    }

    /**
     * 修改通行规则
     */
    @RequiresPermissions("system:rule:edit")
    @Log(title = "通行规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRule rule)
    {
        String permission="";
        for(String s:rule.getCheckList()){
            permission+=s+",";
        }
        if(!"".equals(permission)){
            permission=permission.substring(0,permission.length()-1);
        }
        rule.setPermission(permission);
        if(rule.getPointIds()!=null){
            String pointIds="";

            for(String s:rule.getPointIds()){
                pointIds+=s+",";
            }
            if(!"".equals(pointIds)){
                pointIds=pointIds.substring(0,pointIds.length()-1);
            }
            rule.setPointId(pointIds);
        }
        return toAjax(sysRuleService.updateSysRule(rule));
    }

    /**
     * 删除通行规则
     */
    @RequiresPermissions("system:rule:remove")
    @Log(title = "通行规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysRuleService.deleteSysRuleByIds(ids));
    }

    /**
     * 获取企业模块|菜单权限树
     */
    @GetMapping(value = "/ruleAuth")
    public AjaxResult selectRuleAuthScope() {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("depts", sysRuleService.selectRuleAuthScope());
        return ajax;
    }
}
