package com.persagy.dmp.dic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.dmp.basic.utils.QueryCriteriaHelper;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ResultHelper;
import com.persagy.dmp.dic.entity.DictData;
import com.persagy.dmp.dic.entity.DictType;
import com.persagy.dmp.dic.service.DictDataService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 字典数据
 * @author:linhuili
 * @date:2021/6/10
 */
@RestController
@RequestMapping(value = "/dic/dt/data")
public class DictDataController {

    @Autowired
    private DictDataService service;

    private static final String STATUS="status";
    private static final String VALID="valid";

    /**
     * 查询字典数据
     * @param criteria
     * @return
     */
    @Deprecated
    @PostMapping("/query")
    public CommonResult<List<DictData>> query(@RequestBody QueryCriteria criteria) {
        if(criteria == null) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        QueryWrapper<DictData> wrapper = new QueryWrapper<>();
        ObjectNode objCriteria = criteria.getCriteria();
        if (null!=objCriteria && objCriteria.size()>0){
            // 2021年8月18日19:29:32 兼容旧的逻辑.因为字段改为valid了
            if(objCriteria.has(STATUS)){
                objCriteria.put(VALID,objCriteria.get(STATUS));
                objCriteria.remove(STATUS);
            }
        }
        // 转换查询条件
        QueryCriteriaHelper.toWrapper(wrapper, criteria, DictData.class);
        // 转换查询条件
        Page page = QueryCriteriaHelper.toPage(criteria);
        if(criteria.isOnlyCount()) {
            Integer total = service.queryCount(wrapper);
            page.setTotal(total);
        } else {
            page = service.queryByCondition(QueryCriteriaHelper.toPage(criteria), wrapper);
        }
        return ResultHelper.multi(page.getRecords(), page.getTotal());
    }

    /**
     * 创建字典数据
     * @param vo
     * @return
     */
    @Deprecated
    @PostMapping("/create")
    public CommonResult<DictData> create(@Valid @RequestBody DictData vo){
        vo = service.insert(vo);
        return ResultHelper.single(vo);
    }

    /**
     * 修改字典数据
     * @param vo
     * @return
     */
    @Deprecated
    @PostMapping("/update")
    public CommonResult<DictData> update(@Valid @RequestBody DictData vo){
        vo = service.update(vo);
        return ResultHelper.single(vo);
    }

    /**
     * 删除字典数据
     * @param vo
     * @return
     */
    @Deprecated
    @PostMapping("/delete")
    public CommonResult<DictData> delete(@RequestBody DictData vo){
        if(vo == null){
            throw new BusinessException(ResponseCode.A0400.getCode(),ResponseCode.A0400.getCode());
        }
        if(StringUtils.isEmpty(vo.getId())){
            throw new BusinessException(ResponseCode.A0400.getCode(), "id不能为空");
        }
        service.delete(vo);
        return ResultHelper.success();
    }

    /**
     * 查询映射表替代字典数据的数据集
     * @param dictType
     * @return
     */
    @Deprecated
    @PostMapping("/result")
    public CommonResult<List<DictData>> query(@RequestBody DictType dictType) {
        if(dictType == null) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        List<DictData>  dictDataList = service.queryData(dictType);
        return ResultHelper.multi(dictDataList,dictDataList.size());
    }
}
