package com.fs.store.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.store.dto.StoreOrderProductDTO;
import com.fs.store.param.FsStoreOrderParam;
import com.fs.store.service.*;
import com.fs.store.vo.FsStoreOrderExportVO;
import com.fs.store.vo.FsStoreOrderItemExportVO;
import com.fs.store.vo.FsStoreOrderVO;
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/storeOrder")
public class FsStoreHealthOrderController extends BaseController {
    @Autowired
    private IFsExpressService expressService;
    @Autowired
    private IFsStoreOrderService fsStoreOrderService;
    @Autowired
    private IFsStoreOrderItemService orderItemService;

    @Autowired
    private IFsUserService userService;

    @Autowired
    private IFsStoreOrderStatusService orderStatusService;

    @Autowired
    IErpOrderService erpOrderService;
    @Autowired
    IFsStorePaymentService paymentService;

    @Autowired
    private ICompanyMoneyLogsService moneyLogsService;

    /**
     * 查询健康商城订单列表
     */
    @PreAuthorize("@ss.hasPermi('store:healthStoreOrder:list')")
    @GetMapping("/healthList")
    public TableDataInfo healthStoreList(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("--"));
        }
        param.setIsHealth("1");
        List<FsStoreOrderVO> list = fsStoreOrderService.selectFsStoreOrderListVO(param);
        if (list != null) {
            for (FsStoreOrderVO 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:healthStoreOrder:export')")
    @Log(title = "健康商城订单", businessType = BusinessType.EXPORT)
    @GetMapping("/healthExport")
    public AjaxResult export1(FsStoreOrderParam param) {
        if (param.getBeginTime().equals("") && param.getEndTime().equals("")){
            param.setBeginTime(null);
            param.setEndTime(null);
        }
        if (fsStoreOrderService.isEntityNull(param)){
            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.getDeliverySendTimeRange())){
            param.setDeliverySendTimeList(param.getDeliverySendTimeRange().split("--"));
        }
        if(!StringUtils.isEmpty(param.getDeliveryImportTimeRange())){
            param.setDeliveryImportTimeList(param.getDeliveryImportTimeRange().split("--"));
        }
        param.setIsHealth("1");
        List<FsStoreOrderExportVO> list = fsStoreOrderService.selectFsStoreOrderListVOByExport(param);
        //对手机号脱敏
        if (list != null) {
            for (FsStoreOrderExportVO 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<FsStoreOrderExportVO> util = new ExcelUtil<FsStoreOrderExportVO>(FsStoreOrderExportVO.class);
        return util.exportExcel(list, "订单数据");
    }


    @PreAuthorize("@ss.hasPermi('store:healthStoreOrder:exportItems')")
    @Log(title = "健康商城订单明细导出", businessType = BusinessType.EXPORT)
    @GetMapping("/healthExportItems")
    public AjaxResult exportItems1(FsStoreOrderParam param) {
        if (param.getBeginTime().equals("") && param.getEndTime().equals("")){
            param.setBeginTime(null);
            param.setEndTime(null);
        }
        if (fsStoreOrderService.isEntityNull(param)){
            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.getDeliverySendTimeRange())){
            param.setDeliverySendTimeList(param.getDeliverySendTimeRange().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, "订单明细数据");
    }
}
