package com.fs.hospital.controller.hospital;

import java.util.List;

import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ServletUtils;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.hospital.param.HospitalPharmacyOrderParam;
import com.fs.hospital.vo.HospitalPharmacyOrderListHVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.hospital.domain.HospitalPharmacyOrder;
import com.fs.hospital.service.IHospitalPharmacyOrderService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 药房订单Controller
 *
 * @author fs
 * @date 2024-03-22
 */
@RestController
@RequestMapping("/hospital/hospitalPharmacyOrder")
public class HospitalPharmacyOrderController extends BaseController
{
    @Autowired
    private IHospitalPharmacyOrderService hospitalPharmacyOrderService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询药房订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HospitalPharmacyOrderParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalPharmacyOrderListHVO> list = hospitalPharmacyOrderService.selectHospitalPharmacyOrderListHVO(param);
        return getDataTable(list);
    }

    /**
     * 导出药房订单列表
     */
//    @PreAuthorize("@ss.hasPermi('hospital:hospitalPharmacyOrder:export')")
//    @Log(title = "药房订单", businessType = BusinessType.EXPORT)
//    @GetMapping("/export")
//    public AjaxResult export(HospitalPharmacyOrder hospitalPharmacyOrder)
//    {
//        List<HospitalPharmacyOrder> list = hospitalPharmacyOrderService.selectHospitalPharmacyOrderList(hospitalPharmacyOrder);
//        ExcelUtil<HospitalPharmacyOrder> util = new ExcelUtil<HospitalPharmacyOrder>(HospitalPharmacyOrder.class);
//        return util.exportExcel(list, "药房订单数据");
//    }
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPharmacyOrder:export')")
    @Log(title = "药房订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HospitalPharmacyOrderParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setHospitalId( loginUser.getHospital().getHospitalId());

        List<HospitalPharmacyOrderListHVO> list = hospitalPharmacyOrderService.selectHospitalPharmacyOrderListHVO(param);

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


    /**
     * 获取药房订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPharmacyOrder:query')")
    @GetMapping(value = "/{orderId}")
    public R getInfo(@PathVariable("orderId") Long orderId)
    {

//        return AjaxResult.success(hospitalPharmacyOrderService.selectHospitalPharmacyOrderPVOByOrderId(orderId));
        return hospitalPharmacyOrderService.selectHospitalPharmacyOrderPVOByOrderId(orderId);
    }

    /**
     * 新增药房订单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPharmacyOrder:add')")
    @Log(title = "药房订单", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody HospitalPharmacyOrder hospitalPharmacyOrder)
    {
        return toAjax(hospitalPharmacyOrderService.insertHospitalPharmacyOrder(hospitalPharmacyOrder));
    }

    /**
     * 修改药房订单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPharmacyOrder:edit')")
    @Log(title = "药房订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalPharmacyOrder hospitalPharmacyOrder)
    {
        return toAjax(hospitalPharmacyOrderService.updateHospitalPharmacyOrder(hospitalPharmacyOrder));
    }

    /** 提交发药状态 */

    @Log(title = "药房订单", businessType = BusinessType.UPDATE)
    @PostMapping("/dispensing/{orderId}")
    public AjaxResult UpdateDispensingHospitalPharmacyOrder(@PathVariable Long orderId,@RequestBody int type){
        return  toAjax(hospitalPharmacyOrderService.UpdateDispensingHospitalPharmacyOrder(orderId,type));
    }
    /**
     * 删除药房订单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPharmacyOrder:remove')")
    @Log(title = "药房订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(hospitalPharmacyOrderService.deleteHospitalPharmacyOrderByOrderIds(orderIds));
    }
}
