package com.gree.mall.miniapp.controller.material.parts;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.miniapp.bean.material.parts.PartsSalesOrderBean;
import com.gree.mall.miniapp.constant.Constant;
import com.gree.mall.miniapp.enums.material.BuyPeopleEnum;
import com.gree.mall.miniapp.enums.material.PartsOrderStatusEnum;
import com.gree.mall.miniapp.enums.material.PartsPushFlagEnum;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.material.parts.WebsitPartsSalesLogic;
import com.gree.mall.miniapp.logic.material.parts.WebsitPartsSalesOrderLogic;
import com.gree.mall.miniapp.plus.entity.AdminWebsit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "配件销售订单", tags ={"配件销售订单"} )
@RequestMapping(value = "/app/worker/parts-sales", produces = "application/json; charset=utf-8")
public class SalesOrderController {

    @Resource
    WebsitPartsSalesLogic websitPartsSalesLogic;
    @Resource
    WebsitPartsSalesOrderLogic websitPartsSalesOrderLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ApiOperation(value = "查询师傅未转销售订单网点")
    @PostMapping("/exist/websit/list")
    public ResponseHelper<List<AdminWebsit>> existWebsitList (
            @ApiParam(value = "师傅身份证",required = true) @RequestParam String identity,
            @ApiParam(value = "网点编号") @RequestParam(required = false) String websitId
    ) throws RemoteServiceException {
        return ResponseHelper.success(websitPartsSalesLogic.existWebsitList(identity, websitId));
    }


    @ApiOperation(value = "销售订单列表")
    @PostMapping("/list")
    public ResponseHelper<IPage<PartsSalesOrderBean>> appList(
            @ApiParam(value = "师傅身份证", required = true) @RequestParam String identity,
            @ApiParam(value = "销售订单号") @RequestParam(required = false) String salesId,
            @ApiParam(value = "单据状态 SAVE-保存未提交 SUBMIT-已提交 PAYED-已支付 END-已完成 CANCEL-已取消") @RequestParam(required = false) PartsOrderStatusEnum status,
            @ApiParam(value = "下推标志 SALES=销售 TRANS=转销售 END=完结") @RequestParam(required = false) PartsPushFlagEnum pushFlag,
            @ApiParam(value = "配件编码") @RequestParam(required = false) String partsNumber,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNo,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws RemoteServiceException {
        return ResponseHelper.success(websitPartsSalesLogic.appList(identity, salesId, status, pushFlag, partsNumber, pageNo, pageSize));
    }

    @ApiOperation(value = "销售订单详情")
    @PostMapping("/detail")
    public ResponseHelper<IPage<PartsSalesOrderBean>> appDetail(
            @ApiParam(value = "师傅身份证", required = true) @RequestParam String identity,
            @ApiParam(value = "销售订单号", required = true) @RequestParam String salesId,
            @ApiParam(value = "下推标志 SALES=销售 TRANS=转销售 END=完结") @RequestParam(required = false) String pushFlag
    ) throws RemoteServiceException {
        return ResponseHelper.success(websitPartsSalesLogic.appDetail(identity, salesId, pushFlag));
    }



    @ApiOperation("配件销售单新增")
    @PostMapping("/add")
    public ResponseHelper<PartsSalesOrderBean> add(
            @RequestBody PartsSalesOrderBean partsSalesOrderBean
    ) throws RemoteServiceException {
        partsSalesOrderBean.setOrderSource("ONLINE");
        partsSalesOrderBean.setBuyPeople(BuyPeopleEnum.WORKER.getKey());
        partsSalesOrderBean.setIsApply(Boolean.FALSE);

        websitPartsSalesOrderLogic.add(partsSalesOrderBean);
        return ResponseHelper.success(partsSalesOrderBean);
    }

    @ApiOperation("配件销售单根据id获取")
    @PostMapping("/loadSalesOrderById")
    public ResponseHelper<PartsSalesOrderBean> loadSalesOrderById(
            @RequestParam String id
    ) throws RemoteServiceException {
        return ResponseHelper.success(websitPartsSalesOrderLogic.loadSalesOrderById(id));
    }

    @ApiOperation("提交订单")
    @PostMapping("/submit")
    public ResponseHelper<PartsSalesOrderBean> submit(@ApiParam(value = "销售单id",required = true) @RequestParam String id) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.PARTS_SALES + id);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("销售单冻结中");
        }
        try {
            return ResponseHelper.success(websitPartsSalesOrderLogic.submit(id));
        } catch(Exception e) {
            log.error("【提交订单处理】失败",e);
            throw e;
        } finally {
            obtain.unlock();
        }
    }
    
    @ApiOperation("师傅提货")
	@PostMapping("/workerConfirmDelivery")
	public ResponseHelper workerConfirmDelivery(
			@ApiParam(value = "销售单号",required = true) @RequestParam String id
			) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.PARTS_SALES + id);
        if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("销售单冻结中");
        }
        try {
            websitPartsSalesOrderLogic.workerConfirmDelivery(id);
        } catch(Exception e) {
            log.error("【师傅提货处理】失败",e);
            throw e;
        } finally {
            obtain.unlock();
        }

		return ResponseHelper.success();
	}

}
