package com.bweb.controller;

import com.alibaba.excel.EasyExcel;
import com.bweb.config.SysConst;
import com.bweb.excel.model.ExportTradeOrderModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.EasyExcelUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.config.CommonSysConst;
import com.hfkj.entity.BsPayQrCode;
import com.hfkj.entity.BsTradeOrder;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserInfoModel;
import com.hfkj.service.*;
import com.hfkj.sysenum.PayQrCodeStatusEnum;
import com.hfkj.sysenum.SecUserTypeEnum;
import com.hfkj.sysenum.TradeOrderPayModeEnum;
import com.hfkj.sysenum.TradeOrderStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(value = "交易订单")
@RequestMapping(value = "/tradeOrder")
public class BsTradeOrderController {

    private static Logger log = LoggerFactory.getLogger(BsTradeOrderController.class);

    @Resource
    private UserCenter userCenter;

    @Resource
    private BsTradeOrderService tradeOrderService;

    @Resource
    private BsTradeOrderRefundService tradeOrderRefundService;

    @RequestMapping(value="/getOrderList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询订单列表")
    public ResponseData getOrderList(@RequestParam(value = "salesmanId" , required = false) Long salesmanId,
                                     @RequestParam(value = "merId" , required = false) Long merId,
                                     @RequestParam(value = "merName" , required = false) String merName,
                                     @RequestParam(value = "companyId" , required = false) Long companyId,
                                     @RequestParam(value = "agentId" , required = false) Long agentId,
                                     @RequestParam(value = "storeId" , required = false) Long storeId,
                                     @RequestParam(value = "storeName" , required = false) String storeName,
                                     @RequestParam(value = "payMode" , required = false) String payMode,
                                     @RequestParam(value = "outTradeNo" , required = false) String outTradeNo,
                                     @RequestParam(value = "accTradeNo" , required = false) String accTradeNo,
                                     @RequestParam(value = "payTimeS" , required = false) Long payTimeS,
                                     @RequestParam(value = "payTimeE" , required = false) Long payTimeE,
                                     @RequestParam(value = "status" , required = false) Integer status,
                                     @RequestParam(value = "pageNum" , required = true) Integer pageNum,
                                     @RequestParam(value = "pageSize" , required = true) Integer pageSize) {
        try {
            Map<String, Object> param = new HashMap<>();
            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type0.getNumber())) {
                param.put("companyId", companyId);
                param.put("agentId", agentId);
                param.put("salesmanId", salesmanId);
                param.put("merId", merId);
                param.put("merName", merName);
                param.put("storeId", storeId);
                param.put("storeName", storeName);

            } else if (userModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type1.getNumber())) {
                param.put("companyId", userModel.getBsCompany().getId());
                param.put("agentId", agentId);
                param.put("salesmanId", salesmanId);
                param.put("merId", merId);
                param.put("merName", merName);
                param.put("storeId", storeId);
                param.put("storeName", storeName);

            } else if (userModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type2.getNumber())) {
                param.put("agentId", userModel.getAgent().getId());
                param.put("salesmanId", salesmanId);
                param.put("merId", merId);
                param.put("merName", merName);
                param.put("storeId", storeId);
                param.put("storeName", storeName);

            } else if (userModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type3.getNumber())) {
                param.put("salesmanId", userModel.getSalesman().getId());
                param.put("merId", merId);
                param.put("merName", merName);
                param.put("storeId", storeId);
                param.put("storeName", storeName);

            } else if (userModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type4.getNumber())) {
                param.put("merId", userModel.getMer().getId());
                param.put("storeId", storeId);
                param.put("storeName", storeName);

            } else if (userModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type5.getNumber())) {
                param.put("storeId", userModel.getStore().getId());
            }
            param.put("payMode", payMode);
            param.put("outTradeNo", outTradeNo);
            param.put("accTradeNo", accTradeNo);
            param.put("payTimeS", payTimeS);
            param.put("payTimeE", payTimeE);
            param.put("status", status);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(tradeOrderService.getTradeOrderList(param)));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/exportTradeOrder",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询订单列表")
    public ResponseData exportTradeOrder(@RequestParam(value = "salesmanId" , required = false) Long salesmanId,
                                     @RequestParam(value = "merId" , required = false) Long merId,
                                     @RequestParam(value = "merName" , required = false) String merName,
                                     @RequestParam(value = "companyId" , required = false) Long companyId,
                                     @RequestParam(value = "agentId" , required = false) Long agentId,
                                     @RequestParam(value = "storeId" , required = false) Long storeId,
                                     @RequestParam(value = "storeName" , required = false) String storeName,
                                     @RequestParam(value = "payMode" , required = false) String payMode,
                                     @RequestParam(value = "outTradeNo" , required = false) String outTradeNo,
                                     @RequestParam(value = "accTradeNo" , required = false) String accTradeNo,
                                     @RequestParam(value = "payTimeS" , required = false) Long payTimeS,
                                     @RequestParam(value = "payTimeE" , required = false) Long payTimeE,
                                     @RequestParam(value = "status" , required = false) Integer status) {
        try {
            Map<String, Object> param = new HashMap<>();
            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type0.getNumber())) {
                param.put("companyId", companyId);
                param.put("agentId", agentId);
                param.put("salesmanId", salesmanId);
                param.put("merId", merId);
                param.put("merName", merName);
                param.put("storeId", storeId);
                param.put("storeName", storeName);

            } else if (userModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type1.getNumber())) {
                param.put("companyId", userModel.getBsCompany().getId());
                param.put("agentId", agentId);
                param.put("salesmanId", salesmanId);
                param.put("merId", merId);
                param.put("merName", merName);
                param.put("storeId", storeId);
                param.put("storeName", storeName);

            } else if (userModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type2.getNumber())) {
                param.put("agentId", userModel.getAgent().getId());
                param.put("salesmanId", salesmanId);
                param.put("merId", merId);
                param.put("merName", merName);
                param.put("storeId", storeId);
                param.put("storeName", storeName);

            } else if (userModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type3.getNumber())) {
                param.put("salesmanId", userModel.getSalesman().getId());
                param.put("merId", merId);
                param.put("merName", merName);
                param.put("storeId", storeId);
                param.put("storeName", storeName);

            } else if (userModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type4.getNumber())) {
                param.put("merId", userModel.getMer().getId());
                param.put("storeId", storeId);
                param.put("storeName", storeName);

            } else if (userModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type5.getNumber())) {
                param.put("storeId", userModel.getStore().getId());
            }
            param.put("payMode", payMode);
            param.put("outTradeNo", outTradeNo);
            param.put("accTradeNo", accTradeNo);
            param.put("payTimeS", payTimeS);
            param.put("payTimeE", payTimeE);
            param.put("status", status);


            List<ExportTradeOrderModel> dataList = new ArrayList<>();
            ExportTradeOrderModel data;

            List<BsTradeOrder> tradeOrderList = tradeOrderService.getTradeOrderList(param);
            for (BsTradeOrder tradeOrder : tradeOrderList) {
                data = new ExportTradeOrderModel();
                data.setMerName(tradeOrder.getMerName());
                data.setStoreName(tradeOrder.getStoreName());
                data.setOutTradeNo(tradeOrder.getOutTradeNo());
                data.setPayMode(TradeOrderPayModeEnum.getNameByCode(tradeOrder.getPayMode()).getName());
                data.setTradeAmount(tradeOrder.getTradeAmount());
                data.setStoreDiscountActualPrice(tradeOrder.getStoreDiscountActualPrice());
                data.setUserDiscountActualPrice(tradeOrder.getUserDiscountActualPrice());
                data.setTradeActualAmount(tradeOrder.getTradeActualAmount());
                data.setStatus(TradeOrderStatusEnum.getNameByNumber(tradeOrder.getStatus()).getName());
                data.setCreateTime(tradeOrder.getCreateTime());
                data.setPayTime(tradeOrder.getPayTime());
                data.setPlatformMerNo(tradeOrder.getPlatformMerNo());
                data.setSalesmanName(tradeOrder.getSalesmanName());
                dataList.add(data);
            }

            String fileName = "/temporary/" + System.currentTimeMillis() + ".xlsx";
            EasyExcel.write(CommonSysConst.getSysConfig().getFilesystem() + fileName, ExportTradeOrderModel.class)
                    .sheet("交易订单")
                    .doWrite(dataList);
            return ResponseMsgUtil.success(fileName);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getOrderByOutTradeOrder",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据交易订单号 查询订单信息")
    public ResponseData getOrderByOutTradeOrder(@RequestParam(value = "outTradeOrder" , required = true) String outTradeOrder) {
        try {
            // 查询订单
            BsTradeOrder order = tradeOrderService.getOrderByOutTradeNo(outTradeOrder);
            if (order == null) {
                log.error("configAccount error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到订单");
            }
            Map<String, Object> param = new HashMap<>();
            param.put("order", order);
            param.put("orderRefund", tradeOrderRefundService.getRefundByOutTradeOrder(order.getOutTradeNo()));
            return ResponseMsgUtil.success(param);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
