package com.gree.mall.miniapp.controller.workorder;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.miniapp.bean.workorder.*;
import com.gree.mall.miniapp.constant.Constant;
import com.gree.mall.miniapp.enums.workorder.OrderBaseStatusEnum;
import com.gree.mall.miniapp.enums.workorder.OrderFlagEnum;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.workorder.OrderBaseLogic;
import com.gree.mall.miniapp.logic.workorder.OrderLogLogic;
import com.gree.mall.miniapp.plus.entity.OrderSmallType;
import com.gree.mall.miniapp.plus.entity.PgOrderBase;
import com.gree.mall.miniapp.plus.entity.PgOrderOperatorLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "工单API", tags ={"工单API"} )
@RequestMapping(value = "/pg/order/base", produces = "application/json; charset=utf-8")
public class OrderBaseController {

    @Autowired
    OrderBaseLogic orderBaseLogic;
    @Autowired
    OrderLogLogic orderLogLogic;
    @Autowired
    RedisLockRegistry redisLockRegistry;

    @PostMapping("/list")
    @ApiOperation(value = "师傅的工单列表")
    public ResponseHelper<IPage<OrderBaseVO>> list(
            @ApiParam(value = "关键词",required = false) @RequestParam(required = false) String keyword,
            @ApiParam(value = "延保订单id",required = false) @RequestParam(required = false) String pgIncreOrderId,
            @ApiParam(value = "是否延保单 true=是 false=否",required = false) @RequestParam(required = false) Boolean isYb,
            @ApiParam(value = "是否维保 true=是 false=否",required = false) @RequestParam(required = false) Boolean isWb,
            @ApiParam(value = "DJD=待接单，FWZ=服务中，YCD=异常待处理 YWG=已完工",required = false)@RequestParam(required = false) OrderBaseStatusEnum orderBaseStatus,
            @ApiParam(value = "1=今天 2=明天 3=其他",required = false) @RequestParam(required = false) Integer byDay,
            @ApiParam(value = "开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws Exception{
        IPage<OrderBaseVO> list = orderBaseLogic.list(keyword,pgIncreOrderId,isYb,isWb, orderBaseStatus, byDay, startTime, endTime, pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @PostMapping("/list2")
    @ApiOperation(value = "客户的工单列表")
    public ResponseHelper<IPage<OrderBaseVO>> list(
            @ApiParam(value = "延保订单id",required = false) @RequestParam(required = false) String pgIncreOrderId,
            @ApiParam(value = "是否延保单 true=是 false=否",required = false) @RequestParam(required = false) Boolean isYb,
            @ApiParam(value = "是否维保 true=是 false=否",required = false) @RequestParam(required = false) Boolean isWb,
            @ApiParam(value = "1=待预约，2=服务中，3=待评价 4=已完成",required = false)@RequestParam(required = false) Integer flag,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws Exception{
        IPage<OrderBaseVO> pgOrderBaseIPage = orderBaseLogic.list2(pgIncreOrderId,isYb,isWb,flag, pageNum, pageSize);
        return ResponseHelper.success(pgOrderBaseIPage);
    }




    @PostMapping("/detail")
    @ApiOperation(value = "服务单详情")
    public ResponseHelper<PgOrderBaseDetailVO> detail(
            @RequestParam String orderBaseId
    ) throws Exception{
        PgOrderBaseDetailVO detail = orderBaseLogic.detail(orderBaseId);
        return ResponseHelper.success(detail);
    }


    @PostMapping("/type/list")
    @ApiOperation(value = "工单小类列表")
    public ResponseHelper<List<OrderSmallType>> list(
    ) throws Exception{
        List<OrderSmallType> orderSmallTypes = orderBaseLogic.orderSmallTypeList();
        return ResponseHelper.success(orderSmallTypes);
    }



    @PostMapping("/status/count")
    @ApiOperation(value = "工单状态统计")
    public ResponseHelper<CountOrderStatusBean> countStatus(
            @ApiParam(value = "延保订单id",required = false) @RequestParam(required = false) String pgIncreOrderId,
            @ApiParam(value = "是否延保单 true=是 false=否",required = false) @RequestParam(required = false) Boolean isYb,
               @ApiParam(value = "是否维保单 true=是 false=否",required = false) @RequestParam(required = false) Boolean isWb
    ) throws Exception{
        CountOrderStatusBean countOrderStatusBean = orderBaseLogic.countOrderStatus(pgIncreOrderId,isYb,isWb);
        return ResponseHelper.success(countOrderStatusBean);
    }

    @PostMapping("/my/schedule")
    @ApiOperation(value = "我的日程-未来7天的工单统计")
    public ResponseHelper<MySchedule7Day> mySchedule() throws Exception{
        MySchedule7Day mySchedule7Day = orderBaseLogic.mySchedule();
        return ResponseHelper.success(mySchedule7Day);
    }


    @PostMapping("/save")
    @ApiOperation(value = "创建工单")
    public ResponseHelper save(
            @RequestBody OrderAddBeanDTO orderAddBeanDTO
    ) throws Exception{
        orderBaseLogic.add(orderAddBeanDTO);
        return ResponseHelper.success();
    }


    @PostMapping("/appraise")
    @ApiOperation(value = "评价工单")
    public ResponseHelper save(
            @RequestBody OrderAppraiseBean orderAppraiseBean
    ) throws Exception{
        orderBaseLogic.appraise(orderAppraiseBean);
        return ResponseHelper.success();
    }


    @PostMapping("/rece")
    @ApiOperation("确定接单")
    public ResponseHelper rece(
            @RequestParam String orderBaseId
    ){
        orderBaseLogic.rece(orderBaseId);
        return ResponseHelper.success();
    }

    @PostMapping("/qd")
    @ApiOperation("抢单")
    public ResponseHelper qd(
            @RequestParam String orderBaseId
    ) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + orderBaseId);
        if(!obtain.tryLock(10, TimeUnit.SECONDS)){
            return ResponseHelper.error("系统繁忙，请稍后再试");
        }
        try {
            orderBaseLogic.qd(orderBaseId);
            return ResponseHelper.success();
        }finally {
            obtain.unlock();
        }
    }


    @PostMapping("/flag")
    @ApiOperation(value = "工单-打标记(加急/取消加急,异常/取消异常 等等..)")
    public ResponseHelper flag(
            @ApiParam(value = "工单号",required = true) @RequestParam String orderBaseId,
            @ApiParam(value = "标记类型 JIA_JI=加急 YI_CHANGE=异常") @RequestParam OrderFlagEnum orderFlagEnum,
            @ApiParam(value = "打标记 true=打 false=取消",required = true) @RequestParam Boolean flag
    ){
        orderBaseLogic.jiaji(orderBaseId,orderFlagEnum,flag);
        return ResponseHelper.success();
    }


    @PostMapping("/operator/list")
    @ApiOperation(value = "工单过程反馈列表")
    public ResponseHelper<List<PgOrderOperatorLog>> operatorList(
            @RequestParam String orderBaseId
    ) throws Exception{
        List<PgOrderOperatorLog> pgOrderOperatorLogs = orderLogLogic.operatorLogList(orderBaseId);
        return ResponseHelper.success(pgOrderOperatorLogs);
    }

    @PostMapping("/operator/add")
    @ApiOperation(value = "过程反馈-添加")
    public ResponseHelper addOrderOperator(
            @RequestBody PgOrderOperatorLog pgOrderOperatorLog
    ) {
        orderLogLogic.addLog(pgOrderOperatorLog);
        return ResponseHelper.success();
    }

}
