package com.unis.crk.basic.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unis.crk.basic.entity.BasicEnum;
import com.unis.crk.basic.entity.FuncInfo;
import com.unis.crk.basic.service.IBasicEnumService;
import com.unis.crk.basic.entity.vo.BasicEnumVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.unis.crk.utils.ResultUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 数据字典大类 前端控制器
 * </p>
 *
 * @author TangSong
 * @since 2021-08-04
 */
@Api(value = "数据字典大类",tags = {"数据字典大类"})
@RestController
@RequestMapping("basicEnum")
@CrossOrigin
public class BasicEnumController {

    private final static Logger LOGGER = LoggerFactory.getLogger(BasicEnumController.class);


    @Autowired
    private IBasicEnumService basicEnumService;


    /**
     * 分页高级查询
     * @return
     */
    @ApiOperation(value="查询", notes="分页高级查询")
    @PostMapping(value="/findPage")
    public ResultUtil findPage(@RequestBody BasicEnumVo pageQuery) {
        ResultUtil<IPage<BasicEnum>> data = new ResultUtil<>();
        data.success(this.basicEnumService.findPageVo(pageQuery));
        return data;
    }
    /**
     * 枚举字典数据获取统一对外接口
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/findEnumObj", produces = MediaType.APPLICATION_JSON_VALUE, method=RequestMethod.GET)
    @ApiOperation(value="获取枚举字典数据统一对外接口", notes="获取枚举字典数据统一对外接口")
    public Map<String,Object>  findEnumObj()  {
        Map<String, Object> map = new HashMap<>();
        Map<String , List<BasicEnum>> parentMap = basicEnumService.findEnumObjParentId();
        Map<Object,Object> enumMap = basicEnumService.findEnumObjEnumId();
        map.put("parentMap", parentMap);
        map.put("enumMap", enumMap);
        return map;
    }
    /**
     * 根据id获取所有子节点
     * @param enumid 节点id
     * @return
     */
    @RequestMapping(value="/getAllChildren", method=RequestMethod.GET)
    @ApiOperation(value="根据id获取这个字典下所有子节点", notes="根据id获取这个字典下所有子节点")
    @ApiImplicitParams({
            @ApiImplicitParam(name="enumid", value="字典id", paramType="query"),
            @ApiImplicitParam(name="isQueryFristNode", value="是否只查询第一级", paramType="query")
    })

    public List<BasicEnum> getAllChildren(String  enumid,Boolean isQueryFristNode) {
        List<BasicEnum> list  = basicEnumService.queryAllChildren(enumid,isQueryFristNode);
        return list;
    }

    @ApiOperation(value="根据id获取这个字典第一节点", notes="根据id获取这个字典第一节点")
    @GetMapping ("getChildren/{enumid}/{isQueryFristNode}")
    public ResultUtil getChildren(@PathVariable("enumid") String  enumid,@PathVariable("isQueryFristNode")Boolean isQueryFristNode) {
        ResultUtil<List<BasicEnum>> data = new ResultUtil<>();
        QueryWrapper<BasicEnum> basicEnumQueryWrapper = new QueryWrapper<>();
        basicEnumQueryWrapper.eq("parentId",enumid);
        basicEnumQueryWrapper.select().orderByDesc("parentId").orderByAsc("enumCode");
        List<BasicEnum> list  = basicEnumService.queryAllChildren(enumid,isQueryFristNode);

        return data.success(list);
    }
    /**
     * 查询全部数据
     * @return
     */
    @ApiOperation(value="查询全部父枚举", notes="查询全部父枚举")
    @GetMapping("getAllParen")
    public ResultUtil getAllParen(){
        ResultUtil<List<BasicEnum>> data = new ResultUtil<>();
        List<BasicEnum> list =basicEnumService.getAllParen();
        for(Iterator<BasicEnum> it = list.iterator(); it.hasNext();){
            BasicEnum next = it.next();
            int i=basicEnumService.getCountChildren(next.getEnumId());
            if(i>0){
                next.setSubset(true);

            }else{
                next.setSubset(false);
            }
        }
        data.success(list);
        return data;
    }
    /**
     * 获取原粮化验明细字典树
     *
     * @return
     */
    @RequestMapping(value="/getJymx", method=RequestMethod.GET)
    @ApiOperation(value="获取原粮化验明细字典树", notes="获取原粮化验明细字典树")
    @ApiImplicitParams({
            @ApiImplicitParam(name="lspzCode", value="化验明细品种编码", paramType="query")
    })
    public List<BasicEnum> getJymx(String lspzCode) {
        QueryWrapper<BasicEnum> basicEnumQueryWrapper = new QueryWrapper<>();
        basicEnumQueryWrapper.eq("parentId","1046");
        if (lspzCode.length() >3){
            lspzCode = StrUtil.sub(lspzCode,0,3);
        }
        basicEnumQueryWrapper.eq("enumCode",lspzCode);
        basicEnumQueryWrapper.last("limit 0,1");
        BasicEnum basicEnum = basicEnumService.getOne(basicEnumQueryWrapper); //查询到此类的化验品种
        List<BasicEnum> list = new ArrayList<>();
        if (basicEnum != null){
            list = basicEnumService.queryAllChildren(basicEnum.getEnumId(),false ); //得到化验明细
        }
        return list;
    }
    /**
     * 获取成品粮化验明细字典树
     *
     * @return
     */
    @RequestMapping(value="/getCplJymx", method=RequestMethod.GET)
    @ApiOperation(value="获取成品粮化验明细字典树", notes="获取成品粮化验明细字典树")
    @ApiImplicitParams({
            @ApiImplicitParam(name="cplPzCode", value="化验成品粮品种编码", paramType="query")
    })
    public List<BasicEnum> getCplJymx(String cplPzCode) {
        QueryWrapper<BasicEnum> basicEnumQueryWrapper = new QueryWrapper<>();
        basicEnumQueryWrapper.eq("parentId","95276");
        basicEnumQueryWrapper.eq("enumCode",cplPzCode);
        basicEnumQueryWrapper.last("limit 0,1");
        BasicEnum basicEnum = basicEnumService.getOne(basicEnumQueryWrapper); //查询到此类的化验品种
        List<BasicEnum> list = new ArrayList<>();
        if (basicEnum != null){
            list = basicEnumService.queryAllChildren(basicEnum.getEnumId(),false ); //得到化验明细
        }
        return list;
    }
    /**
     * 获取成品粮化验明标准子字典树
     *
     * @return
     */
    @RequestMapping(value="/getCplJyzz", method=RequestMethod.GET)
    @ApiOperation(value="获取成品粮化验明细字典树", notes="获取成品粮化验明标准子字典树")
    @ApiImplicitParams({
            @ApiImplicitParam(name="cplPzCode", value="化验成品粮品种编码", paramType="query")
    })
    public List<BasicEnum> getCplJyzz(String cplPzCode) {
        QueryWrapper<BasicEnum> basicEnumQueryWrapper = new QueryWrapper<>();
        basicEnumQueryWrapper.eq("parentId","95281");
        basicEnumQueryWrapper.eq("enumCode",cplPzCode);
        basicEnumQueryWrapper.last("limit 0,1");
        BasicEnum basicEnum = basicEnumService.getOne(basicEnumQueryWrapper); //查询到此类的化验品种
        List<BasicEnum> list = basicEnumService.queryAllChildren(basicEnum.getEnumId(),false ); //得到化验明细
        return list;
    }

    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil getOne(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
        ResultUtil<BasicEnum> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.basicEnumService.getById(id));
        return data;
    }


    /**
     * 查询全部数据
     * @return
     */
    @ApiOperation(value="查询", notes="查询全部数据")
    @GetMapping("getAll")
    public ResultUtil getAll(){
        ResultUtil<List<BasicEnum>> data = new ResultUtil<>();
        data.success(this.basicEnumService.list());
        return data;
    }


    /**
     * 新增一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    public ResultUtil add(@RequestBody BasicEnum param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
            data.failed("所接收对象为null");
            return data;
        }
        if (basicEnumService.getById(param.getEnumId()) != null){
            data.failed("枚举ID已存在");
            return data;
        }
        param.setUpdatetime(null);
        data.success(this.basicEnumService.save(param));
        return data;
    }


    /**
     * 修改一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    public ResultUtil edit(@RequestBody BasicEnum param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
            data.failed("所接收对象为null");
            return data;
        }
        param.setUpdatetime(null);
        data.success(this.basicEnumService.updateById(param));
        return data;
    }
    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @ApiOperation(value="删除", notes="根据ID删除数据")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable("id") String id){
        LOGGER.info("入参为:{}", id);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("所接收id为null");
            return data;
        }
        data.success(this.basicEnumService.removeById(id));
        return data;
    }
    /**
     * 移植库级字段
     * @param parentId
     * @return
     */
    @ApiOperation(value="移植库级字段", notes="根据parentId数据")
    @GetMapping("moveDepotEnum/{parentId}")
    public ResultUtil moveDepotEnum(@PathVariable("parentId") String parentId){
        LOGGER.info("入参为:{}", parentId);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (StringUtils.isEmpty(parentId)){
            data.failed("所接收id为null");
            return data;
        }
        data.success(this.basicEnumService.moveDepotEnum(parentId));
        return data;
    }


}

