package com.willalp.web.controller.level;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.StringUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.level.domain.HsSysLevel;
import com.willalp.level.service.IHsSysLevelService;
import com.willalp.web.demo.treetable.entity.TreeTable;
import org.springframework.security.access.prepost.PreAuthorize;
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.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.enums.BusinessType;

import javax.servlet.http.HttpServletRequest;


/**
 * 层级规则Controller
 *
 * @author bjh
 * @date 2021-12-02
 */
@RestController
@RequestMapping("/level/rule")
public class HsSysLevelController extends BaseController
{
    @Autowired
    private IHsSysLevelService hsSysLevelService;

    /**
     * 查询层级规则列表
     */
/*    @PreAuthorize("@ss.hasPermi('level:rule:list')")
    @GetMapping("/list")
    public AjaxResult list(HsSysLevels hsSysLevel)
    {
        List<HsSysLevels> list = hsSysLevelService.selectHsSysLevelList(hsSysLevel);
        return AjaxResult.success(list);
    }*/

    /**
     * 查询层级规则列表
     */
    @PreAuthorize("@ss.hasPermi('level:rule:list')")
    @GetMapping("/list")
    public AjaxResult list(HsSysLevel hsSysLevel)
    {
       //if(hsSysLevel.getLevelcode()==null && hsSysLevel.getLevelname()==null ){
           hsSysLevel.setAncestors("");
           hsSysLevel.setParentId(null);

       //}
        List<HsSysLevel> treeNodeList = hsSysLevelService.selectHsSysLevelList(hsSysLevel);
        return AjaxResult.success(treeNodeList);
    }

   /* @PreAuthorize("@ss.hasPermi('level:rule:list')")
    @GetMapping("/list")
    public AjaxResult list(HttpServletRequest request)
    {
        *//*QueryWrapper<HsSysLevel> entityWrapper = new QueryWrapper<>();
        entityWrapper.orderByDesc( "create_time");
        String name = request.getParameter("levelname");
        if (!StringUtils.isEmpty(name)) {
            entityWrapper.like("levelname", name);
        }
        String levelcode = request.getParameter("levelcode");
        if (!StringUtils.isEmpty(levelcode)) {
            entityWrapper.like("levelcode", levelcode);
        }*//*
        List<HsSysLevel> treeNodeList = hsSysLevelService.list();
        return AjaxResult.success(treeNodeList);
    }*/

    /**
     * 导出层级规则列表
     */
    @PreAuthorize("@ss.hasPermi('level:rule:export')")
    @Log(title = "层级规则", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsSysLevel hsSysLevel)
    {
        List<HsSysLevel> list = hsSysLevelService.selectHsSysLevelList(hsSysLevel);
        ExcelUtil<HsSysLevel> util = new ExcelUtil<HsSysLevel>(HsSysLevel.class);
        return util.exportExcel(list, "层级规则数据");
    }

    /**
     * 获取层级规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('level:rule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        //return AjaxResult.success(hsSysLevelService.selectHsSysLevelById(id));
        return AjaxResult.success(hsSysLevelService.getById(id));
    }

    /**
     * 新增层级规则
     */
    @PreAuthorize("@ss.hasPermi('level:rule:add')")
    @Log(title = "层级规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsSysLevel hsSysLevel)
    {
        Long id = hsSysLevelService.getByMaxId();
        hsSysLevel.setId(id);
        hsSysLevel.setCreateBy(SecurityUtils.getUsername());
        hsSysLevel.setCreateTime(DateUtils.getNowDate());
        hsSysLevel.setSfzx("0");
        return toAjax(hsSysLevelService.save(hsSysLevel));
    }

    /**
     * 修改层级规则
     */
    @PreAuthorize("@ss.hasPermi('level:rule:edit')")
    @Log(title = "层级规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsSysLevel hsSysLevel)
    {
        hsSysLevel.setUpdateBy(SecurityUtils.getUsername());
        hsSysLevel.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsSysLevelService.updateById(hsSysLevel));
    }

    /**
     * 删除层级规则
     */
    @PreAuthorize("@ss.hasPermi('level:rule:remove')")
    @Log(title = "层级规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        Long id = hsSysLevelService.getParent(ids[0]);
        if(id>0){
            return AjaxResult.error(" 删除失败，该层级含有子节点");
        }else{
            Long code = hsSysLevelService.getCode(ids[0]);
            if(code>0){
                return AjaxResult.error(" 删除失败，该层级已定义编码规则");
            }
        }
        return toAjax(hsSysLevelService.removeByIds(Arrays.asList(ids)));
    }
}
