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

import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.common.NotExamineDto;
import cn.com.ty.lift.ud.operation.controller.query.UserRebateRuleQuery;
import cn.com.ty.lift.ud.operation.controller.query.VipFeeSettingQuery;
import cn.com.ty.lift.ud.operation.mapper.entity.UserRebateRuleEntity;
import cn.com.ty.lift.ud.operation.service.IUserRebateRuleService;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 用户返利规则;API
 * 
 * @author Xson
 * @date 2020-06-28
 */
@Api(tags = "用户返利规则 API - 小陶")
@RestController
@RequestMapping("/api/userRebateRules")
public class UserRebateRuleController extends BaseController<UserRebateRuleEntity,UserRebateRuleQuery> {

    private final Logger log = LoggerFactory.getLogger(UserRebateRuleController.class);
    
    private @Autowired IUserRebateRuleService userRebateRuleService;

    @Override
    protected IService<UserRebateRuleEntity> getService() {
        return userRebateRuleService;
    }

    @ApiOperation(value = "审批 1:通过 2:驳回")
    @PostMapping("/examine")
    public RestResponse examine(@RequestBody NotExamineDto neDto) {
        return userRebateRuleService.examineById(neDto);
    }


    /**
     * 重写修改
     *
     * @param request
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    @Override
    public RestResponse update(@RequestBody UserRebateRuleEntity request) {
        request.setExamineFlag(0);
        boolean updateFlag = getService().updateById(request);
        UserRebateRuleEntity e = getService().getById(request.getId());
        if (updateFlag) {
            return RestResponse.success(null, "修改成功");
        } else {
            return RestResponse.fail("修改失败");
        }
    }

    /**
     * 新增提交
     *
     * @param entity
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增提交")
    @Override
    public RestResponse create(@RequestBody UserRebateRuleEntity entity) {
        BigDecimal upLine = new BigDecimal("1");
        if(null == entity.getRoleId()){
            return RestResponse.fail("角色不能为空");
        }
        if(null == entity.getRebate1() || entity.getRebate1().compareTo(upLine) >= 0){
            return RestResponse.fail("一级返利不能为空或者不能大于等于1");
        }
        if(null == entity.getRebate2() || entity.getRebate2().compareTo(upLine) >= 0){
            return RestResponse.fail("二级返利不能为空或者不能大于等于1");
        }
        return userRebateRuleService.create(entity);
    }

    /**
     * 新增保存
     *
     * @param entity
     * @return
     */
    @PostMapping("/insert-save")
    @ApiOperation(value = "新增保存")
    public RestResponse createSave(@RequestBody UserRebateRuleEntity entity) {
        BigDecimal upLine = new BigDecimal("1");
        if(null == entity.getRoleId()){
            return RestResponse.fail("角色不能为空");
        }
        if(null == entity.getRebate1() || entity.getRebate1().compareTo(upLine) >= 0){
            return RestResponse.fail("一级返利不能为空或者不能大于1");
        }
        if(null == entity.getRebate2() || entity.getRebate2().compareTo(upLine) >= 0){
            return RestResponse.fail("二级返利不能为空或者不能大于1");
        }
        entity.setExamineFlag(3);
        boolean saveFlag = getService().save(entity);
        if (saveFlag) {
            return RestResponse.success(entity.getId(), "保存成功");
        } else {
            return RestResponse.fail("保存失败");
        }
    }

    /**
     * 提交操作
     *
     * @param id
     * @return
     */
    @PostMapping("/submit/{id}")
    @ApiOperation(value = "提交操作")
    public RestResponse submit(@PathVariable("id") Long id) {
        if(null == id){
            return RestResponse.fail("id不能为空");
        }
        return userRebateRuleService.submit(id);
    }

    /**
     * 重写条件查询的方法
     * @param request
     * @return
     */
    @ApiOperation(value = "列表分页查询")
    @PostMapping("/query-page")
    @Override
    public RestResponse getCondition(@RequestBody UserRebateRuleQuery request) {
        if(request.getPageNum() < 1){
            return RestResponse.fail("error", "当前页不能小于1");
        }
        return userRebateRuleService.selectByExample(request);
    }

}