package cn.com.ty.lift.ud.tableDict.controller;

import java.math.BigDecimal;
import java.util.List;

import cn.com.ty.lift.ud.tableDict.controller.query.TableDictQuery;
import cn.com.ty.lift.ud.tableDict.mapper.entity.TableDictVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.service.IService;

import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.tableDict.mapper.entity.TableDictDto;
import cn.com.ty.lift.ud.tableDict.mapper.entity.TableDictEntity;
import cn.com.ty.lift.ud.tableDict.service.ITableDictService;
import cn.com.xwy.boot.web.dto.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 数据字典表 API
 * 
 * @author Xson
 * @date 2020-05-07
 */
@Api(tags = "数据字典表 API")
@RestController
@RequestMapping("/api/tableDicts")
public class TableDictController extends BaseController<TableDictEntity, TableDictQuery> {

    private final Logger log = LoggerFactory.getLogger(TableDictController.class);
    
    private @Autowired ITableDictService tableDictService;

    @Override
    protected IService<TableDictEntity> getService() {
        return tableDictService;
    }

    /**
     * 根据表名和字段查询常量
     * @param request
     * @return
     */
    @PostMapping("/query-constant")
    @ApiOperation(value = "根据表名和字段查询常量")
    public RestResponse getConstantByTableAndColumn(@RequestBody TableDictDto request) {
        List<TableDictVo> result = tableDictService.getConstantByTableAndColumn(request);
        return RestResponse.success(result, "查询成功");

    }

    /**
     * 新建重写
     * @param entity
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    @Override
    public RestResponse create(@RequestBody TableDictEntity entity) {
        if(null != entity.getVal()){
            //打赏金额最低1元
            BigDecimal bigDecimal = new BigDecimal(entity.getVal());
            bigDecimal = bigDecimal.setScale(2, BigDecimal.ROUND_FLOOR);
            if(entity.getTableName().equals("reward_setting") && entity.getField().equals("reward")
                    && bigDecimal.compareTo(new BigDecimal("1.00")) == -1){
                return RestResponse.fail("打赏金额不能少于1元");
            }
        }

        boolean saveFlag = getService().save(entity);
        if (saveFlag) {
            return RestResponse.success(null, "新增成功");
        }else {
            return RestResponse.fail("新增失败");
        }
    }

    /**
     * 修改重写
     * @param request
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    @Override
    public RestResponse update(@RequestBody TableDictEntity request) {
        if(null != request.getVal()){
            //打赏金额最低1元
            BigDecimal bigDecimal = new BigDecimal(request.getVal());
            bigDecimal = bigDecimal.setScale(2, BigDecimal.ROUND_FLOOR);
            if(request.getTableName().equals("reward_setting") && request.getField().equals("reward")
                    && bigDecimal.compareTo(new BigDecimal("1.00")) == -1){
                return RestResponse.fail("打赏金额不能少于1元");
            }
        }
        boolean updateFlag = getService().updateById(request);
        if (updateFlag) {
            return RestResponse.success(null, "修改成功");
        }else {
            return RestResponse.fail("修改失败");
        }
    }

}