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.DictType;
import com.persagy.dmp.dic.service.DictTypeService;
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/type")
public class DictTypeController {

    @Autowired
    private DictTypeService service;

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

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

    /**
     * 字典类型删除
     * @param vo
     * @return
     */
    @Deprecated
    @PostMapping("/delete")
    public CommonResult<DictType> delete(@RequestBody DictType 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();
    }
}
