package io.renren.modules.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.R;
import io.renren.modules.business.entity.BusinessDictEntity;
import io.renren.modules.business.service.BusinessDictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 数据字典表
 *
 * @author jiaqiao
 * @email jiaqiao@163.com
 * @date 2022-07-15 11:57:22
 */
@RestController
@RequestMapping("business/businessdict")
public class BusinessDictController {
    @Autowired
    private BusinessDictService businessDictService;

    /**
     * 后台列表
     */
    @RequestMapping("/backList")
    @RequiresPermissions("business:businessdict:list")
    public List<BusinessDictEntity> list(){
        QueryWrapper<BusinessDictEntity> queryWrapper = new QueryWrapper<BusinessDictEntity>();
        queryWrapper.ne("parent_id", "-1");
        List<BusinessDictEntity> dictList = businessDictService.list(queryWrapper);
        HashMap<Integer, BusinessDictEntity> dictMap = new HashMap<>(12);
        for(BusinessDictEntity d : dictList){
            dictMap.put(d.getDictId(),d);
        }
        for(BusinessDictEntity d : dictList){
            BusinessDictEntity parent = dictMap.get(d.getParentId());
            if (Objects.nonNull(parent)) {
                d.setParentName(parent.getName());
            }
        }
        return dictList;
    }

    /**
     * 选择菜单(添加、修改菜单)
     */
    @GetMapping("/select")
    @RequiresPermissions("business:businessdict:list")
    public R select(){
        //查询列表数据
        QueryWrapper<BusinessDictEntity> queryWrapper = new QueryWrapper<BusinessDictEntity>();
        queryWrapper.ne("parent_id", "-1");
        List<BusinessDictEntity> dictList = businessDictService.list(queryWrapper);
        //添加顶级菜单
        BusinessDictEntity root = new BusinessDictEntity();
        root.setDictId(0);
        root.setName("一级字典");
        root.setParentId(0);
        root.setOpen(false);
        dictList.add(root);
        return R.ok().put("dictList", dictList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:businessdict:list")
    public R list(@RequestParam Map<String, Object> params){
        // PageUtils page = businessDictService.queryPage(params);
        List<BusinessDictEntity> businessDictEntitys = businessDictService.listWithTree();
        return R.ok().put("data", businessDictEntitys);
    }

    /**
     * 获取一级标题菜单列表
     * @param params
     * @return
     */
    @RequestMapping("/getOneList")
    @RequiresPermissions("business:businessdict:list")
    public R getOneList(@RequestParam Map<String, Object> params){
        // PageUtils page = businessDictService.queryPage(params);
        List<BusinessDictEntity> businessDictGetOneList = businessDictService.getOneList();
        return R.ok().put("data", businessDictGetOneList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{dictId}")
    @RequiresPermissions("business:businessdict:info")
    public R info(@PathVariable("dictId") Integer dictId){
        BusinessDictEntity dictEntity = businessDictService.getById(dictId);
        return R.ok().put("data", dictEntity);
    }

    /**
     * 通过名字查条数
     */
    @RequestMapping("/findCnByName")
    @RequiresPermissions("business:businessdict:info")
    public R findCnByName(@RequestParam Map<String, Object> params){
        Integer i = 0;
        if(params.get("id")!=null&&!params.get("id").equals("")){
            String id = params.get("id").toString();
            BusinessDictEntity dictEntity = businessDictService.getById(Integer.valueOf(id));
            String name = params.get("name").toString();
            if(!dictEntity.getName().equals(name)){
                i = businessDictService.findCnByName(params);
            }
        }else{
            i = businessDictService.findCnByName(params);
        }
        return R.ok().put("data", i);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:businessdict:save")
    public R save(@RequestBody BusinessDictEntity businessDict){
		businessDictService.save(businessDict);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:businessdict:update")
    public R update(@RequestBody BusinessDictEntity businessDict){
		businessDictService.updateById(businessDict);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:businessdict:delete")
    public R delete(@RequestBody Integer[] dictIds){
		businessDictService.removeByIds(Arrays.asList(dictIds));

        return R.ok();
    }

}
