package com.boco.nbd.wios.flow.controller;

import cn.hutool.core.lang.Assert;
import com.boco.nbd.wios.flow.entity.vo.AppOrderDetailVO;
import com.boco.nbd.wios.flow.entity.vo.AppOrderInstallVO;
import com.boco.nbd.wios.flow.entity.vo.AppOrderVO;
import com.boco.nbd.wios.flow.entity.vo.AppPayBillVO;
import com.boco.nbd.wios.flow.service.IAppOrderService;
import com.boco.nbd.wios.manage.entity.bo.MaterialConfig;
import com.boco.nbd.wios.manage.entity.bo.OrderScoreDTO;
import com.boco.nbd.wios.manage.service.impl.OrderScoreService;
import com.boco.nbd.wios.manage.service.impl.OrderService;
import com.ihidea.component.api.v2.BaseResponse;
import com.ihidea.core.support.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @author:cao hai
 * @date:2022/10/27 10:35
 * @version:V1.0
 * @description:CAMS APP 接口保留原先一期格式规范 也没需求,接口格式还不让调整,拷贝原先的代码实现
 * @modify:
 */
@RestController
@Slf4j
@RequestMapping("/api2")
@Api(tags = "CamsApp接口")
public class AppOrderController {

    @Autowired
    private IAppOrderService appOrderService;

    @Autowired
    private OrderScoreService orderScoreService;

    @Autowired
    private OrderService orderService;

    /**
     * 查询订单评分
     *
     * @return
     */
    @GetMapping(value = "orderScore/getOrderScoreByOrderId")
    @ApiOperation(value = "查询订单评分")
    @ApiImplicitParam(paramType = "query", name = "orderId", value = "订单表ID", required = true, dataType = "String")
    public BaseResponse<Object> list(String orderId) {
        return new BaseResponse<>(orderScoreService.getOrderScoreByOrderId(orderId));
    }

    /**
     * 保存订单回访信息
     * description
     *
     * @param list
     */
    @PostMapping(value = "orderScore/save")
    @ApiOperation(value = "保存订单回访信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "订单id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "visitContent", value = "意见或建议", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "files", value = "图片地址,多个逗号分割", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "list", value = "格式:[{\"id\":9,\"score\":70,\"description\":\"描述\"},{\"id\":12,\"score\":60,\"description\":\"描述\"}]", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "knowAfterSalesChannel", value = "是否了解墙盒售后问题反馈渠道:0-否,1:是", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "installLength", value = "墙盒线缆的安装长度", dataType = "int", paramType = "query", required = true),
    })
    public BaseResponse<Object> save(String orderId, String visitContent, String files, String list, Integer knowAfterSalesChannel, Integer installLength) {
        return orderService.saveOrderScore(orderId, visitContent, files, list, knowAfterSalesChannel, installLength);
    }

    @GetMapping(value = "order/qryList")
    @ApiOperation(value = "查询订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "camsPhone", value = "cams手机号", dataType = "String", paramType = "query", required = true),})
    public BaseResponse<List<AppOrderVO>> getList(String camsPhone) {
        Assert.notEmpty(camsPhone, "手机号不能为空");
        return new BaseResponse<>(appOrderService.queryOrderList(camsPhone));
    }

    @GetMapping(value = "order/qryOrderDetail")
    @ApiOperation(value = "查询订单详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "工单号", dataType = "String", paramType = "query", required = true),})
    public BaseResponse<AppOrderDetailVO> qryOrderDetail(String orderId) {
        Assert.notEmpty(orderId, "工单号不能为空");
        AppOrderDetailVO result = appOrderService.getOrderDetail(orderId);
        return new BaseResponse<>(result);
    }

    @GetMapping(value = "order/qryInstallDetail")
    @ApiOperation(value = "查询安装订单详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "工单号", dataType = "String", paramType = "query", required = true),})
    public BaseResponse<AppOrderInstallVO> qryInstallDetail(String orderId) {
        Assert.notEmpty(orderId, "工单号不能为空");
        AppOrderInstallVO orderInstallDTO = appOrderService.getInstallDetail(orderId);
        return new BaseResponse<>(orderInstallDTO);
    }

    @GetMapping(value = "order/qryExpandIntro")
    @ApiOperation(value = "查询报装说明")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "工单号", dataType = "String", paramType = "query", required = true),})
    public BaseResponse<String> getExpandIntro(String orderId) {
        Assert.notEmpty(orderId, "缺少参数");
        MaterialConfig mc = appOrderService.getExpandIntro(orderId);
        if (mc != null) {
            return new BaseResponse<>(mc.getContent());
        } else {
            return new BaseResponse<>();
        }
    }

    /**
     * 查询账单列表
     *
     * @param camsPhone
     * @param orderId
     * @param status
     * @return
     */
    @GetMapping(value = "order/qryBillList")
    @ApiOperation(value = "查询账单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "camsPhone", value = "cams手机号", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "orderId", value = "工单号", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "status", value = "状态 0-未支付 1-已支付", dataType = "int", paramType = "query", required = false),})
    public BaseResponse<List<AppPayBillVO>> qryBillList(String camsPhone, String orderId, Integer status) {
        if (!StringUtils.isEmpty(camsPhone) && !StringUtils.isEmpty(orderId)) {
            throw new ServiceException("cams手机号和工单号不能同时为空");
        }
        List<AppPayBillVO> list = appOrderService.qryBill(camsPhone, orderId, status);
        return new BaseResponse<>(list);
    }


    /**
     * 报装
     *
     * @param orderId
     * @param expandStatus
     * @param expandFailReason
     * @param expandFailOperate
     */
    @PostMapping(value = "order/expand")
    @ApiOperation(value = "报装")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "订单id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "expandStatus", value = "报装状态 1:通过 2:不通过", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "expandFailReason", value = "报装不通过原因", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "expandFailOperate", value = "报装不通过后订单后续操作 1:换其他用电类型继续安装  2:不安装了", dataType = "int", paramType = "query", required = false),})
    public BaseResponse<Object> expand(String orderId, Integer expandStatus, String expandFailReason, Integer expandFailOperate) {
        if (StringUtils.isEmpty(orderId) || expandStatus == null) {
            throw new ServiceException("缺少参数");
        }
        Integer[] items = {1, 2};
        if (!Arrays.asList(items).contains(expandStatus)) {
            throw new ServiceException("状态不合法");
        }
        appOrderService.expand(orderId, expandStatus, expandFailReason, expandFailOperate);
        return new BaseResponse<>();
    }

}