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.VipFeeSettingQuery;
import cn.com.ty.lift.ud.operation.mapper.entity.VipFeeSettingEntity;
import cn.com.ty.lift.ud.operation.service.IVipFeeSettingService;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.aliyun.oss.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 会费设置;API
 * 
 * @author Xson
 * @date 2020-06-19
 */
@Api(tags = "会费设置 API - 小陶")
@RestController
@RequestMapping("/api/vipFeeSettings")
public class VipFeeSettingController extends BaseController<VipFeeSettingEntity,VipFeeSettingQuery> {

    private final Logger log = LoggerFactory.getLogger(VipFeeSettingController.class);
    
    private @Autowired
    IVipFeeSettingService vipFeeSettingService;

    @Override
    protected IService<VipFeeSettingEntity> getService() {
        return vipFeeSettingService;
    }

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


    /**
     * 重写修改
     *
     * @param request
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    @Override
    public RestResponse update(@RequestBody VipFeeSettingEntity request) {
        VipFeeSettingEntity level = vipFeeSettingService.getByPeriodAndLevel(request.getPeriod(), request.getUserLevelId());
        if(null != level && !request.getId().equals(level.getId())){
            return RestResponse.fail("同一种vip的有效期时长不能重复");
        }
        request.setExamineFlag(0);
        boolean updateFlag = getService().updateById(request);
        VipFeeSettingEntity 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 VipFeeSettingEntity entity) {
        if(null == entity.getName()){
            return RestResponse.fail("等级名称不能为空");
        }
        if(null == entity.getPrice()){
            return RestResponse.fail("价格不能为空");
        }
        if(null == entity.getPeriod()){
            return RestResponse.fail("有效期不能为空");
        }
        if(null == entity.getUserLevelId()){
            return RestResponse.fail("用户等级不能为空");
        }
        return vipFeeSettingService.create(entity);
    }

    /**
     * 新增保存
     *
     * @param entity
     * @return
     */
    @PostMapping("/insert-save")
    @ApiOperation(value = "新增保存")
    public RestResponse createSave(@RequestBody VipFeeSettingEntity entity) {
        if(null == entity.getName()){
            return RestResponse.fail("等级名称不能为空");
        }
        if(null == entity.getPrice()){
            return RestResponse.fail("价格不能为空");
        }
        if(null == entity.getPeriod()){
            return RestResponse.fail("有效期不能为空");
        }
        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 vipFeeSettingService.submit(id);
    }

    @GetMapping("/query-all/{userLevelId}")
    @ApiOperation(value = "用户类型下所有会费")
    public RestResponse getAll(@PathVariable("userLevelId") Integer userLevelId){

        return vipFeeSettingService.getAll(userLevelId);
    }

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

}