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

import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.liftCaseAppeal.controller.query.LiftCaseAppealQuery;
import cn.com.ty.lift.ud.liftCaseAppeal.mapper.entity.CancelSeasonDto;
import cn.com.ty.lift.ud.liftCaseAppeal.mapper.entity.LiftCaseAppealDto;
import cn.com.ty.lift.ud.liftCaseAppeal.mapper.entity.LiftCaseAppealEntity;
import cn.com.ty.lift.ud.liftCaseAppeal.service.ILiftCaseAppealService;
import cn.com.ty.lift.ud.utils.CurrentUserInfo;
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 javax.servlet.http.HttpServletRequest;

/**
 * 诊单申诉 API
 *
 * @author Xson
 * @date 2020-04-23
 */
@Api(tags = "诊单申诉 API- 小陶")
@RestController
@RequestMapping("/api/liftCaseAppeals")
public class LiftCaseAppealController extends BaseController<LiftCaseAppealEntity, LiftCaseAppealQuery> {

    private final Logger log = LoggerFactory.getLogger(LiftCaseAppealController.class);

    private @Autowired
    ILiftCaseAppealService liftCaseAppealService;

    @Override
    protected IService<LiftCaseAppealEntity> getService() {
        return liftCaseAppealService;
    }


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

    @ApiOperation(value = "驳回 - 差 发送消息给该用户")
    @PostMapping("/cancel")
    public RestResponse cancel(@RequestBody CancelSeasonDto cancelDto) {
        if(null == cancelDto.getCancelSeason()){
            RestResponse.fail("驳回原因不能为空");
        }
        liftCaseAppealService.cancelById(cancelDto);
        return RestResponse.success("驳回成功", "驳回成功");
    }

    @ApiOperation(value = "结束申诉")
    @PostMapping("/endAppeal/{id}")
    public RestResponse endAppeal(@PathVariable("id") Long id) {
        liftCaseAppealService.endAppeal(id);
        return RestResponse.success(null, "结束申诉成功");
    }

    @ApiOperation(value = "退款 ")
    @PostMapping("/refund")
    public RestResponse refund(@RequestBody LiftCaseAppealDto lcaDto) {
        if(null == lcaDto.getId()){
            return RestResponse.fail("申诉ID不能为空");
        }
        if(null == lcaDto.getUserId()){
            return RestResponse.fail("申诉人不能为空");
        }
        return liftCaseAppealService.refund(lcaDto.getId(),lcaDto.getUserId());
    }

    /**
     * 重写新增的方法
     * @param entity
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    @Override
    public RestResponse<LiftCaseAppealEntity> create(@RequestBody LiftCaseAppealEntity entity) {
        if(null == entity.getAppealerId()){
            return RestResponse.fail("申诉人不能为空");
        }
        if(null == entity.getDataId()){
            return RestResponse.fail("诊单不能为空");
        }
        return liftCaseAppealService.create(entity);
    }
}