package com.gree.mall.manager.controller.order;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ApiNotAuth;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.CommonTemplateVO;
import com.gree.mall.manager.bean.order.OrderShareVO;
import com.gree.mall.manager.enums.OrderShareStatusEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.order.OrderShareLogic;
import com.gree.mall.manager.plus.entity.OrderShare;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
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.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.rmi.RemoteException;

@Slf4j
@RestController
@Api(value = "订单结算管理", tags = {"订单结算管理"})
@RequestMapping(value = "/order/share", produces = "application/json; charset=utf-8")
public class OrderShareController {

    @Autowired
    OrderShareLogic orderShareLogic;

    @ZfireList
    @PostMapping("/list/pageV2")
    @ApiOperation(value = "订单结算管理列表V2")
    public ResponseHelper<IPage<OrderShareVO>> pageV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<OrderShareVO> page = orderShareLogic.pageV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<OrderShareVO>() {});
    }

    @PostMapping("/pageExport")
    @ApiOperation(value = "订单结算管理板列表V2导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<OrderShareVO> page = orderShareLogic.pageV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseHelper<Page<OrderShare>> list(HttpServletRequest request,
            @ApiParam(value = "用户id", required = false) @RequestParam(required = false) String userId,
            @ApiParam(value = "网点id", required = false) @RequestParam(required = false) String websitId,
            @ApiParam(value = "分销结算id", required = false) @RequestParam(required = false) String orderShareId,
            @ApiParam(value = "订单号", required = false) @RequestParam(required = false) String orderId,
            @ApiParam(value = "用户名", required = false) @RequestParam(required = false) String workUserName,
            @ApiParam(value = "手机号", required = false) @RequestParam(required = false) String phone,
            @ApiParam(value = "结算状态: ING=结算中  OVER=已结算  CANCEL=已取消 EXCEPTION=异常结算", required = false) @RequestParam(required = false) String status,
            @ApiParam(value = "开始创建时间", required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束创建时间", required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "开始结算时间", required = false) @RequestParam(required = false) String startSettleTime,
            @ApiParam(value = "结束结算时间", required = false) @RequestParam(required = false) String endSettleTime,
             @ApiParam(value = "支付开始时间", required = false) @RequestParam(required = false) String startPayTime,
             @ApiParam(value = "结束开始时间", required = false) @RequestParam(required = false) String endPayTime,
            @ApiParam(value = "页号", required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam(required = true) Integer pageSize,
             @ApiParam(value = "职位", required = false) @RequestParam(required = false) String position
    ) throws RemoteServiceException {
        IPage<OrderShare> list = orderShareLogic
                .list(request,userId, websitId, orderShareId, orderId, workUserName, phone, status, startTime, endTime,
                        startSettleTime, endSettleTime,position,
                        startPayTime,endPayTime,
                        pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @GetMapping("/confirm")
    @ApiOperation("管理员手动确认分佣")
    public ResponseHelper confirm(@ApiParam(value = "订单号", required = false) @RequestParam(required = false) String orderId) throws RemoteServiceException {
        orderShareLogic.confirm(orderId);
        return ResponseHelper.success();
    }


    @PostMapping("/remark")
    @ApiOperation("备注")
    public ResponseHelper remark(
            @ApiParam(value = "结算id", required = true) @RequestParam(required = true) String orderShareId,
            @ApiParam(value = "备注内容", required = true) @RequestParam(required = true) String remark
    ) throws RemoteServiceException {
        orderShareLogic.remark(orderShareId,remark);
        return ResponseHelper.success();
    }


    @GetMapping("/export")
    @ApiOperation("结算导出")
    public void export(HttpServletRequest request, HttpServletResponse response,
                       @ApiParam(value = "用户id", required = false) @RequestParam(required = false) String userId,
                       @ApiParam(value = "分销结算id", required = false) @RequestParam(required = false) String orderShareId,
                       @ApiParam(value = "订单号", required = false) @RequestParam(required = false) String orderId,
                       @ApiParam(value = "网点id", required = false) @RequestParam(required = false) String websitId,
                       @ApiParam(value = "用户名", required = false) @RequestParam(required = false) String workUserName,
                       @ApiParam(value = "手机号", required = false) @RequestParam(required = false) String phone,
                       @ApiParam(value = "结算状态: ING=结算中  OVER=已结算  CANCEL=已取消 EXCEPTION=异常结算", required = false) @RequestParam(required = false) String status,
                       @ApiParam(value = "开始创建时间", required = false) @RequestParam(required = false) String startTime,
                       @ApiParam(value = "结束创建时间", required = false) @RequestParam(required = false) String endTime,
                       @ApiParam(value = "开始结算时间", required = false) @RequestParam(required = false) String startSettleTime,
                       @ApiParam(value = "结束结算时间", required = false) @RequestParam(required = false) String endSettleTime,
                       @ApiParam(value = "支付开始时间", required = false) @RequestParam(required = false) String startPayTime,
                       @ApiParam(value = "结束开始时间", required = false) @RequestParam(required = false) String endPayTime,
                       @ApiParam(value = "职位", required = false) @RequestParam(required = false) String position
    ) throws Exception {
        orderShareLogic.export2(request, response, userId, websitId, orderShareId, orderId, workUserName, phone, status,
                startTime, endTime, startSettleTime, endSettleTime,position,startPayTime,endPayTime);
    }


    @ApiNotAuth
    @GetMapping("/worker/ordernum")
    @ApiOperation("师傅本月的有效订单数量")
    public ResponseHelper<Integer> workerOrderNum(
            @ApiParam(value = "师傅手机号", required = true) @RequestParam(required = true) String mobile
    ) throws RemoteServiceException {
        Integer shareNum = orderShareLogic.getShareNum(mobile);
        return ResponseHelper.success(shareNum);
    }

    @PostMapping("/status")
    @ApiOperation("变更状态-线下结算-取消结算")
    public ResponseHelper status(
            @ApiParam(value = "结算id", required = true) @RequestParam(required = true) String orderShareId,
            @ApiParam(value = "OFFLINE=线下结算 CANCEL=取消结算", required = true) @RequestParam(required = true)OrderShareStatusEnum orderShareStatus
    ) {
        orderShareLogic.updateOrderStatus(orderShareId,orderShareStatus.toString());
        return ResponseHelper.success();
    }

}
