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

import cn.com.ty.lift.ud.chat.controller.model.LiftCaseReq;
import cn.com.ty.lift.ud.chat.controller.query.LiftCaseQuery;
import cn.com.ty.lift.ud.chat.mapper.entity.LiftCaseEntity;
import cn.com.ty.lift.ud.chat.service.ILiftCaseService;
import cn.com.ty.lift.ud.common.BaseController;
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.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
 * 诊单 API Jing
 *
 * @author Xson
 * @date 2020-04-23
 */
@Api(tags = "诊单 API- 盛文静")
@RestController
@RequestMapping("/api/liftCases")
public class LiftCaseController extends BaseController<LiftCaseEntity, LiftCaseQuery> {

    private final Logger log = LoggerFactory.getLogger(LiftCaseController.class);
    @Autowired
    private ILiftCaseService liftCaseService;

    @Override
    protected IService<LiftCaseEntity> getService() {
        return liftCaseService;
    }

    /**
     * 创建诊单
     *
     * @param req
     * @return
     */
    @ApiOperation(value = "创建诊单")
    @PostMapping(value = "/createLiftCase")
    public RestResponse createLiftCase(@Valid @RequestBody LiftCaseReq req, BindingResult result) {
        // validated 后台校验
        if (result.hasErrors()) {
            List<ObjectError> list = result.getAllErrors();
            for (ObjectError error : list) {
                if (StringUtils.isNotEmpty(error.getDefaultMessage())) {
                    return RestResponse.fail(error.getDefaultMessage());
                }
            }
        }

        return liftCaseService.createLiftCase(req);
    }

    @PostMapping("/query-page")
    @ApiOperation(value = "分页查询")
    @Override
    public RestResponse getCondition(@RequestBody LiftCaseQuery request) {
        return liftCaseService.selectByExample(request);
    }

    @PostMapping("/queryPageByInvoiceId")
    @ApiOperation(value = "根据发票id查诊单")
    public RestResponse queryPageByInvoiceId(@RequestBody LiftCaseQuery request) {
        return liftCaseService.queryPageByInvoiceId(request);
    }

    @PostMapping("/findOneByUserIdAndSessionId")
    @ApiOperation(value = "根据诊单id以及用户ID查询诊单详情。")
    public RestResponse findOneByUserIdAndSessionId(@RequestBody LiftCaseQuery request) {
        LiftCaseEntity entity = liftCaseService.getOne(request.condition());
        return RestResponse.success(entity, "Queried successfully");
    }

    @PostMapping("/generateOrder")
    @ApiOperation(value = "问诊专属：完善诊单，创建订单")
    public RestResponse generateOrder(@RequestBody LiftCaseQuery request) {
        return liftCaseService.generateOrder(request);
    }

    @PostMapping("/generateRoom")
    @ApiOperation(value = "出诊专属：完善诊单，创建房间。")
    public RestResponse generateRoom(@RequestBody LiftCaseQuery request) {
        return liftCaseService.generateRoom(request);
    }

    @GetMapping("/platformServiceFee")
    @ApiOperation(value = "获取平台问诊服务费")
    public RestResponse platformServiceFee() {
        return liftCaseService.platformServiceFee();
    }

    @PostMapping("/inviteExperts")
    @ApiOperation(value = "邀请专家接单")
    public RestResponse inviteExperts(@RequestBody LiftCaseEntity request) {
        return liftCaseService.inviteExperts(request);
    }

    @GetMapping("/refusedExperts")
    @ApiOperation(value = "拒绝接单")
    public RestResponse refusedExperts(Long id) {
        return liftCaseService.refusedExperts(id);
    }

    // 根据id查详情
    @GetMapping("/query/{id}")
    @ApiOperation(value = "获取详情")
    @Override
    public RestResponse get(@PathVariable Long id) {
        return liftCaseService.queryDetail(id);
    }

    @GetMapping("/userMakeSureCase")
    @ApiOperation(value = "用户确认诊单")
    public RestResponse userMakeSureCase(Long id) {
        return liftCaseService.userMakeSureCase(id);
    }

    @PostMapping("/toOffer")
    @ApiOperation(value = "报价")
    public RestResponse toOffer(@RequestBody LiftCaseEntity liftCaseEntity) {
        return liftCaseService.toOffer(liftCaseEntity);
    }

    @PostMapping("/chargeMakeSureCase")
    @ApiOperation(value = "专家确认诊单")
    public RestResponse chargeMakeSureCase(@RequestBody LiftCaseEntity liftCaseEntity) {
        return liftCaseService.chargeMakeSureCase(liftCaseEntity);
    }

    @PostMapping("/chargeToClock")
    @ApiOperation(value = "专家打卡")
    public RestResponse chargeToClock(@RequestBody LiftCaseEntity liftCaseEntity) {
        if (liftCaseEntity.getBeforeRepair() == null) {
            return RestResponse.fail("请上传现场情况视频");
        }
        return liftCaseService.chargeToClock(liftCaseEntity);
    }

    @GetMapping("/userToClock")
    @ApiOperation(value = "用户确认打卡")
    public RestResponse userToClock(Long id) {
        return liftCaseService.userToClock(id);
    }

    @GetMapping("/getAllCaseToInvoice")
    @ApiOperation(value = "待开票订单")
    public RestResponse getAllCaseToInvoice(Long userId) {
        return liftCaseService.getAllCaseToInvoice(userId);
    }

    @GetMapping("/getExpertFee")
    @ApiOperation(value = "计算专家应该获得的服务费")
    public BigDecimal getExpertFee(Long id) {
        return liftCaseService.getExpertFee(id);
    }

    @PutMapping("/invitationExpert")
    @ApiOperation(value = "邀请专家")
    public RestResponse invitationExpert(@RequestBody LiftCaseEntity liftCaseEntity) {
        return liftCaseService.invitationExpert(liftCaseEntity);
    }

    @PutMapping("/updateCustomerServiceId")
    @ApiOperation(value = "更新客服id")
    public RestResponse updateCustomerServiceId(@RequestBody LiftCaseEntity liftCaseEntity) {
        return liftCaseService.updateCustomerServiceId(liftCaseEntity);
    }

    @GetMapping("/getMyCase")
    @ApiOperation(value = "我的接单")
    public RestResponse getMyCase(Long userId) {
        return liftCaseService.getMyCase(userId);
    }

    @GetMapping("/cancelCase")
    @ApiOperation(value = "取消订单")
    public RestResponse cancelCase(Long id) {
        return liftCaseService.cancelCase(id);
    }


}