package com.fs.hisStore.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.service.ICompanyMoneyLogsService;
import com.fs.erp.service.IErpOrderService;
import com.fs.his.service.IFsExpressService;
import com.fs.his.service.IFsUserService;
import com.fs.hisStore.dto.StoreOrderProductDTO;
import com.fs.hisStore.param.FsStoreOrderParam;
import com.fs.hisStore.service.IFsStoreOrderItemScrmService;
import com.fs.hisStore.service.IFsStoreOrderScrmService;
import com.fs.hisStore.service.IFsStoreOrderStatusScrmService;
import com.fs.hisStore.service.IFsStorePaymentScrmService;
import com.fs.hisStore.vo.FsPromotionOrderVO;
import com.fs.hisStore.vo.FsStoreOrderItemExportVO;
import com.fs.hisStore.vo.FsStoreOrderPromotionExportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/store/store/storeOrder")
public class FsStoreOrderPromotionScrmController extends BaseController {
    @Autowired
    private IFsExpressService expressService;
    @Autowired
    private IFsStoreOrderScrmService fsStoreOrderService;
    @Autowired
    private IFsStoreOrderItemScrmService orderItemService;

    @Autowired
    private IFsUserService userService;

    @Autowired
    private IFsStoreOrderStatusScrmService orderStatusService;

    @Autowired
    IErpOrderService erpOrderService;
    @Autowired
    IFsStorePaymentScrmService paymentService;

    @Autowired
    private ICompanyMoneyLogsService moneyLogsService;

    /**
     * 查询推广订单列表
     */
    @PreAuthorize("@ss.hasPermi('store:PromotionOrder:list')")
    @GetMapping("/promotionList")
    public TableDataInfo list(FsStoreOrderParam param) {
        startPage();
        if(!StringUtils.isEmpty(param.getCreateTimeRange())){
            param.setCreateTimeList(param.getCreateTimeRange().split("--"));
        }
        if(!StringUtils.isEmpty(param.getPayTimeRange())){
            param.setPayTimeList(param.getPayTimeRange().split("--"));
        }
        if(!StringUtils.isEmpty(param.getDeliveryImportTimeRange())){
            param.setDeliveryImportTimeList(param.getDeliveryImportTimeRange().split("--"));
        }
        if(!StringUtils.isEmpty(param.getDeliverySendTimeRange())){
            param.setDeliverySendTimeList(param.getDeliverySendTimeRange().split("--"));
        }

        List<FsPromotionOrderVO> list = fsStoreOrderService.selectFsPromotionOrderListVO(param);
        if (list != null) {
            for (FsPromotionOrderVO vo : list) {
                if(vo.getPhone()!=null){
                    vo.setPhone(vo.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                    vo.setUserPhone(vo.getUserPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                }

            }
        }
        return getDataTable(list);
    }

    /**
     * 导出推广订单列表
     */
    @PreAuthorize("@ss.hasPermi('store:promotionOrder:export')")
    @Log(title = "导出推广订单", businessType = BusinessType.EXPORT)
    @GetMapping("/promotionExport")
    public AjaxResult export(FsStoreOrderParam param) {
        if (param == null){
            return AjaxResult.error("请筛选数据导出");
        }
        if(!StringUtils.isEmpty(param.getCreateTimeRange())){
            param.setCreateTimeList(param.getCreateTimeRange().split("--"));
        }
        if(!StringUtils.isEmpty(param.getPayTimeRange())){
            param.setPayTimeList(param.getPayTimeRange().split("--"));
        }
        if(!StringUtils.isEmpty(param.getDeliveryImportTimeRange())){
            param.setDeliveryImportTimeList(param.getDeliveryImportTimeRange().split("--"));
        }
        List<FsStoreOrderPromotionExportVO> list = fsStoreOrderService.selectFsPromotionOrderListVOByExport(param);
        //对手机号脱敏
        if (list != null) {
            for (FsStoreOrderPromotionExportVO vo : list) {
                if (vo.getPhone() != null) {
                    vo.setPhone(vo.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                }
                if (vo.getUserPhone() != null) {
                    vo.setUserPhone(vo.getUserPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                }

            }
        }
        ExcelUtil<FsStoreOrderPromotionExportVO> util = new ExcelUtil<FsStoreOrderPromotionExportVO>(FsStoreOrderPromotionExportVO.class);
        return util.exportExcel(list, "订单数据");
    }


    @PreAuthorize("@ss.hasPermi('store:promotionOrder:exportItems')")
    @Log(title = "推广订单明细导出", businessType = BusinessType.EXPORT)
    @GetMapping("/promotionExportItems")
    public AjaxResult exportItems(FsStoreOrderParam param) {
        if(!StringUtils.isEmpty(param.getCreateTimeRange())){
            param.setCreateTimeList(param.getCreateTimeRange().split("--"));
        }
        if(!StringUtils.isEmpty(param.getPayTimeRange())){
            param.setPayTimeList(param.getPayTimeRange().split("--"));
        }
        if(!StringUtils.isEmpty(param.getDeliveryImportTimeRange())){
            param.setDeliveryImportTimeList(param.getDeliveryImportTimeRange().split("--"));
        }
        param.setIsHealth("1");
        List<FsStoreOrderItemExportVO> list = orderItemService.selectFsStoreOrderItemListExportVO(param);
        //对手机号脱敏
        if (list != null) {
            for (FsStoreOrderItemExportVO vo : list) {
                if (vo.getUserPhone() != null) {
                    String phone = vo.getUserPhone().replaceAll("(\\d{3})\\d*(\\d{1})", "$1****$2");
                    vo.setUserPhone(phone);
                }
                if (!StringUtils.isEmpty(vo.getJsonInfo())) {
                    try {
                        StoreOrderProductDTO orderProductDTO = JSONObject.parseObject(vo.getJsonInfo(), StoreOrderProductDTO.class);
                        BeanUtil.copyProperties(orderProductDTO, vo);
                    } catch (Exception e) {
                    }
                }
            }
        }
        ExcelUtil<FsStoreOrderItemExportVO> util = new ExcelUtil<FsStoreOrderItemExportVO>(FsStoreOrderItemExportVO.class);
        return util.exportExcel(list, "订单明细数据");
    }
}
