package com.fs.his.controller;

import java.util.List;

import com.fs.his.domain.FsStoreOrder;
import com.fs.his.param.FsStoreOrderParam;
import com.fs.his.vo.FsStoreSubOrderListVO;
import com.fs.his.vo.FsStoreSubOrderVO;
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.FsStoreSubOrder;
import com.fs.his.service.IFsStoreSubOrderService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 订单Controller
 *
 * @author fs
 * @date 2024-05-09
 */
@RestController
@RequestMapping("/his/storeSubOrder")
public class FsStoreSubOrderController extends BaseController
{
    @Autowired
    private IFsStoreSubOrderService fsStoreSubOrderService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('his:storeSubOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreOrderParam fsStoreSubOrder)
    {
        startPage();
        List<FsStoreSubOrderListVO> list = fsStoreSubOrderService.selectFsStoreSubOrderList(fsStoreSubOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('his:storeSubOrder:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsStoreOrderParam fsStoreSubOrder)
    {
        if (fsStoreSubOrderService.isEntityNull(fsStoreSubOrder)){
            return AjaxResult.error("请筛选数据导出");
        }
        Long count = fsStoreSubOrderService.selectFsStoreSubOrderListCount(fsStoreSubOrder);
        if (count>30000){
            return AjaxResult.error("导出数据不可超过3w条");
        }
        List<FsStoreSubOrderListVO> list = fsStoreSubOrderService.selectFsStoreSubOrderList(fsStoreSubOrder);
        ExcelUtil<FsStoreSubOrderListVO> util = new ExcelUtil<FsStoreSubOrderListVO>(FsStoreSubOrderListVO.class);
        return util.exportExcel(list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:storeSubOrder:query')")
    @GetMapping(value = "/{subOrderId}")
    public AjaxResult getInfo(@PathVariable("subOrderId") Long subOrderId)
    {
        FsStoreSubOrderVO order = fsStoreSubOrderService.selectFsStoreSubOrderVOBySubOrderId(subOrderId);
        if (order.getPhone()!=null){
            order.setPhone(order.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        if (order.getUserPhone()!=null){
            order.setUserPhone(order.getUserPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        return AjaxResult.success(order);
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('his:storeSubOrder:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsStoreSubOrder fsStoreSubOrder)
    {
        return toAjax(fsStoreSubOrderService.insertFsStoreSubOrder(fsStoreSubOrder));
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('his:storeSubOrder:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreSubOrder fsStoreSubOrder)
    {
        return toAjax(fsStoreSubOrderService.updateFsStoreSubOrder(fsStoreSubOrder));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('his:storeSubOrder:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{subOrderIds}")
    public AjaxResult remove(@PathVariable String[] subOrderIds)
    {
        return toAjax(fsStoreSubOrderService.deleteFsStoreSubOrderBySubOrderIds(subOrderIds));
    }
}
