package com.fs.his.controller;

import java.util.Date;
import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.utils.ParseUtils;
import com.fs.common.utils.SecurityUtils;
import com.fs.his.domain.FsExportTask;
import com.fs.his.mapper.FsStorePaymentMapper;
import com.fs.his.param.FsPackageOrderParam;
import com.fs.his.service.IFsExportTaskService;
import com.fs.his.vo.FsPackageOrderExcelVO;
import com.fs.his.vo.FsPackageOrderListVO;
import com.fs.his.vo.FsPackageOrderVO;
import com.fs.his.vo.FsStoreOrderVO;
import com.fs.ybPay.service.IPayService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.his.domain.FsPackageOrder;
import com.fs.his.service.IFsPackageOrderService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

import static com.fs.his.utils.PhoneUtil.*;

/**
 * 套餐订单Controller
 *
 * @author fs
 * @date 2023-08-25
 */
@RestController
@RequestMapping("/his/packageOrder")
public class FsPackageOrderController extends BaseController
{
    @Autowired
    private IFsPackageOrderService fsPackageOrderService;
    @Autowired
    private FsStorePaymentMapper fsStorePaymentMapper;
    @Autowired
    private IPayService payService;
    @Autowired
    private IFsExportTaskService exportTaskService;
    /**
     * 查询套餐订单列表
     */
    @PreAuthorize("@ss.hasPermi('his:packageOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsPackageOrderParam  fsPackageOrder)
    {
        startPage();
        if (fsPackageOrder.getPhoneMk()!=null&&fsPackageOrder.getPhoneMk()!=""){
            fsPackageOrder.setPhone(encryptPhone(fsPackageOrder.getPhoneMk()));
        }
        List<FsPackageOrderListVO> list = fsPackageOrderService.selectFsPackageOrderListVO(fsPackageOrder);
        return getDataTable(list);
    }

    /**
     * 导出套餐订单列表
     */
    @PreAuthorize("@ss.hasPermi('his:packageOrder:export')")
    @Log(title = "套餐订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsPackageOrderParam  fsPackageOrder)
    {
        Integer exportType1 = exportTaskService.isExportType1(SecurityUtils.getUserId());
        if (exportType1>0){
            return AjaxResult.error("你已经有正在导出的任务");
        }
        logger.info("导出套餐订单:"+ SecurityUtils.getUserId());
        if (fsPackageOrderService.isEntityNull(fsPackageOrder)){
            return AjaxResult.error("请筛选数据导出");
        }
        Long count = fsPackageOrderService.selectFsPackageOrderExcelListVOCount(fsPackageOrder);
        if (count>30000){
            return AjaxResult.error("导出数据不可超过3w条");
        }
        FsExportTask task=new FsExportTask();
        task.setTaskType(2);
        task.setStatus(0);
        task.setStartTime(new Date());
        task.setRemark("套餐包导出");
        task.setSysType(1);
        task.setUserId(SecurityUtils.getUserId());
        exportTaskService.insertFsExportTask(task);
        fsPackageOrder.setTaskId(task.getTaskId());

        fsPackageOrderService.exportData(fsPackageOrder);
        return new AjaxResult(200,"后台正在导出,请等待...任务ID:"+task.getTaskId(),task.getTaskId());

    }

    /**
     * 获取套餐订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:packageOrder:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        FsPackageOrderVO fsPackageOrderVO = fsPackageOrderService.selectFsPackageOrderByOrderIdVO(orderId);
        if (fsPackageOrderVO.getPhone() != null && fsPackageOrderVO.getPhone().length() > 11) {
            fsPackageOrderVO.setPhone(decryptPhoneMk(fsPackageOrderVO.getPhone()));
        } else {
            fsPackageOrderVO.setPhone(ParseUtils.parsePhone(fsPackageOrderVO.getPhone()));
        }
        return AjaxResult.success(fsPackageOrderVO);
    }

    @GetMapping(value = "/queryPhone/{orderId}")
    @Log(title = "查看电话", businessType = BusinessType.GRANT)
    @PreAuthorize("@ss.hasPermi('his:packageOrder:queryPhone')")
    public R getPhone(@PathVariable("orderId") Long orderId)
    {
        FsPackageOrderVO order = fsPackageOrderService.selectFsPackageOrderByOrderIdVO(orderId);
        String phone = order.getPhone();
        if (phone!=null&&phone.length()>11){
            phone=decryptPhone(phone);
        }
        return R.ok().put("data",phone);
    }

    /**
     * 新增套餐订单
     */
    @PreAuthorize("@ss.hasPermi('his:packageOrder:add')")
    @Log(title = "套餐订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsPackageOrder fsPackageOrder)
    {
        return toAjax(fsPackageOrderService.insertFsPackageOrder(fsPackageOrder));
    }
    @GetMapping("/payment/{orderId}")
    public AjaxResult getPayInfo(@PathVariable("orderId") Long orderId)
    {
        return AjaxResult.success(fsPackageOrderService.selectFsPackagePaymentByOrderId(orderId));
    }
    /**
     * 修改套餐订单
     */
    @PreAuthorize("@ss.hasPermi('his:packageOrder:edit')")
    @Log(title = "套餐订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsPackageOrder fsPackageOrder)
    {
        return toAjax(fsPackageOrderService.updateFsPackageOrder(fsPackageOrder));
    }


//    /**
//     * 退款
//     */
//    @PreAuthorize("@ss.hasPermi('his:packageOrder:refund')")
//    @GetMapping("refund/{orderId}")
//    public AjaxResult refund(@PathVariable("orderId") Long orderId)
//    {
//        return AjaxResult.success(fsPackageOrderService.PackageOrderRefund(orderId));
//    }

    /**
     * 从套餐包退款问诊
     * @param orderId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('his:packageOrder:inquiryRefund')")
    @GetMapping("inquiryRefund/{orderId}")
    public AjaxResult inquiryRefund(@PathVariable("orderId") Long orderId)
    {
        return AjaxResult.success(fsPackageOrderService.PackageInquiryOrderRefund(orderId));
    }

    /**
     * 套餐包退款商品
     * @param orderId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('his:packageOrder:productRefund')")
    @GetMapping("storeRefund/{orderId}")
    public AjaxResult storeRefund(@PathVariable("orderId") Long orderId)
    {
        return AjaxResult.success(fsPackageOrderService.PackageStoreOrderRefund(orderId));
    }

    /**
     * 删除套餐订单
     */
    @PreAuthorize("@ss.hasPermi('his:packageOrder:remove')")
    @Log(title = "套餐订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(fsPackageOrderService.deleteFsPackageOrderByOrderIds(orderIds));
    }
}
