package com.fs.hisStore.controller;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
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.domain.R;
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.his.domain.FsIntegralGoods;
import com.fs.his.domain.FsIntegralOrder;
import com.fs.his.dto.ExpressInfoDTO;
import com.fs.his.enums.ShipperCodeEnum;
import com.fs.his.mapper.FsIntegralGoodsMapper;
import com.fs.his.param.FsIntegralOrderCreateParam;
import com.fs.his.param.FsIntegralOrderParam;
import com.fs.his.service.IFsExpressService;
import com.fs.his.service.IFsIntegralOrderService;
import com.fs.his.utils.PhoneUtil;
import com.fs.his.vo.FsIntegralOrderListVO;
import com.fs.his.vo.FsIntegralOrderPVO;
import com.fs.his.vo.FsStoreProductDeliverExcelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

import static com.fs.his.utils.PhoneUtil.decryptAutoPhoneMk;
import static com.fs.his.utils.PhoneUtil.decryptPhone;

/**
 * 积分商品订单Controller
 *
 * @author fs
 * @date 2023-11-02
 */
@RestController
@RequestMapping("/his/integralOrder")
public class FsIntegralOrderController extends BaseController
{
    @Autowired
    private IFsIntegralOrderService fsIntegralOrderService;
    @Autowired
    private IFsExpressService expressService;

    @Autowired
    private FsIntegralGoodsMapper fsIntegralGoodsMapper;
    /**
     * 查询积分商品订单列表
     */
    @PreAuthorize("@ss.hasPermi('his:integralOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsIntegralOrderParam fsIntegralOrder)
    {
        startPage();
        List<FsIntegralOrderListVO> list = fsIntegralOrderService.selectFsIntegralOrderListVO(fsIntegralOrder);
        for (FsIntegralOrderListVO vo : list) {
            vo.setUserPhone(decryptAutoPhoneMk(vo.getUserPhone()));
        }
        return getDataTable(list);
    }

    /**
     * 导出积分商品订单列表
     */
    @PreAuthorize("@ss.hasPermi('his:integralOrder:export')")
    @Log(title = "积分商品订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsIntegralOrder fsIntegralOrder) {
        return fsIntegralOrderService.export(fsIntegralOrder);
    }
    /**
     * 发货
     */
//    @PreAuthorize("@ss.hasPermi('his:integralOrder:sendGoods')")
    @PutMapping("/sendGoods")
    public AjaxResult sendGoods(@RequestBody FsIntegralOrder fsIntegralOrder)
    {
        return toAjax(fsIntegralOrderService.sendGoods(fsIntegralOrder));
    }

    @GetMapping("/importTemplate")
    public AjaxResult sendExport()
    {
        ExcelUtil<FsStoreProductDeliverExcelVO> util = new ExcelUtil<>(FsStoreProductDeliverExcelVO.class);
        return util.importTemplateExcel("导入运单号");
    }
    @Log(title = "导入运单号", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<FsStoreProductDeliverExcelVO> util = new ExcelUtil<>(FsStoreProductDeliverExcelVO.class);
        List<FsStoreProductDeliverExcelVO> list = util.importExcel(file.getInputStream());
        String message = fsIntegralOrderService.importProductDeliver(list);
        return AjaxResult.success(message);
    }
//    @PreAuthorize("@ss.hasPermi('his:integralOrder:express')")
    @GetMapping(value = "/getExpress/{id}")
    public R getExpress(@PathVariable("id") Long id)
    {
        FsIntegralOrder fsIntegralOrder = fsIntegralOrderService.selectFsIntegralOrderByOrderId(id);
        ExpressInfoDTO expressInfoDTO=null;
        if(StringUtils.isNotEmpty(fsIntegralOrder.getDeliverySn())){
            String lastFourNumber = "";
            if (fsIntegralOrder.getDeliveryCode().equals(ShipperCodeEnum.SF.getValue())) {

                lastFourNumber = fsIntegralOrder.getUserPhone();
                if (lastFourNumber.length() == 11) {
                    lastFourNumber = StrUtil.sub(lastFourNumber, lastFourNumber.length(), -4);
                }
            }
            expressInfoDTO=expressService.getExpressInfo(fsIntegralOrder.getOrderCode(),fsIntegralOrder.getDeliveryCode(),fsIntegralOrder.getDeliverySn(),lastFourNumber);
        }
        return R.ok().put("data",expressInfoDTO);
    }
    /**
     * 获取积分商品订单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('his:integralOrder:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        FsIntegralOrderPVO order = fsIntegralOrderService.selectFsIntegralOrderPVO(orderId);

        order.setUserPhone(decryptAutoPhoneMk(order.getUserPhone()));
        return AjaxResult.success(order);
    }

    @GetMapping(value = "/queryPhone/{orderId}")
    @Log(title = "积分订单电话", businessType = BusinessType.GRANT)
    @PreAuthorize("@ss.hasPermi('his:integralOrder:queryPhone')")
    public R getPhone(@PathVariable("orderId") Long orderId)
    {
        FsIntegralOrderPVO order = fsIntegralOrderService.selectFsIntegralOrderPVO(orderId);
        String userPhone = order.getUserPhone();
        if (userPhone.length()>11){
            userPhone = decryptPhone(userPhone);
        }
        return R.ok().put("userPhone",userPhone);
    }

    /**
     * 新增积分商品订单
     */
//    @PreAuthorize("@ss.hasPermi('his:integralOrder:add')")
    @Log(title = "积分商品订单", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody FsIntegralOrderCreateParam param)
    {
        return fsIntegralOrderService.createOrder(param);
    }

    /**
     * 修改积分商品订单
     */
//    @PreAuthorize("@ss.hasPermi('his:integralOrder:edit')")
    @Log(title = "积分商品订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsIntegralOrder fsIntegralOrder)
    {
        return toAjax(fsIntegralOrderService.updateFsIntegralOrder(fsIntegralOrder));
    }

    /**
     * 删除积分商品订单
     */
//    @PreAuthorize("@ss.hasPermi('his:integralOrder:remove')")
    @Log(title = "积分商品订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(fsIntegralOrderService.deleteFsIntegralOrderByOrderIds(orderIds));
    }
}
