package com.persagy.dmp.dic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.DictDataMap;
import com.persagy.dmp.dic.service.DictDataMapService;
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/map")
public class DictDataMapController {

    @Autowired
    private DictDataMapService service;

    /**
     * 查询字典数据映射
     * @param criteria
     * @return
     */
    @Deprecated
    @PostMapping("/query")
    public CommonResult<List<DictDataMap>> query(@RequestBody QueryCriteria criteria) {
        if(criteria == null) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        QueryWrapper<DictDataMap> wrapper = new QueryWrapper<>();
        // 转换查询条件
        QueryCriteriaHelper.toWrapper(wrapper, criteria, DictDataMap.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<DictDataMap> create(@Valid @RequestBody DictDataMap vo){
        vo = service.insert(vo);
        return ResultHelper.single(vo);
    }

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

    /**
     * 删除字典数据映射
     * @param vo
     * @return
     */
    @Deprecated
    @PostMapping("/delete")
    public CommonResult<DictDataMap> delete(@RequestBody DictDataMap 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();
    }
}
