package com.fs.hospital.controller.hospital;

import java.util.HashMap;
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.domain.HospitalOrderItem;
import com.fs.hospital.domain.HospitalOrderItemLogs;
import com.fs.hospital.domain.HospitalTreatOrderLogs;
import com.fs.hospital.param.HospitalOutpatientOrderFinishParam;
import com.fs.hospital.param.HospitalTreatOrderAddParam;
import com.fs.hospital.param.HospitalTreatOrderExecuteParam;
import com.fs.hospital.param.HospitalTreatOrderHParam;
import com.fs.hospital.service.IHospitalOrderItemLogsService;
import com.fs.hospital.service.IHospitalTreatOrderLogsService;
import com.fs.hospital.vo.HospitalOrderItemListHVO;
import com.fs.hospital.vo.HospitalTreatOrderHVO;
import com.fs.hospital.vo.HospitalTreatOrderListHVO;
import com.fs.hospital.vo.HospitalTreatOrderLogsHVO;
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.HospitalTreatOrder;
import com.fs.hospital.service.IHospitalTreatOrderService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 执行Controller
 *
 * @author fs
 * @date 2024-03-22
 */
@RestController
@RequestMapping("/hospital/hospitalTreatOrder")
public class HospitalTreatOrderController extends BaseController
{
    @Autowired
    private IHospitalTreatOrderService hospitalTreatOrderService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IHospitalTreatOrderLogsService hospitalTreatOrderLogsService;

    @Autowired
    private IHospitalOrderItemLogsService iHospitalOrderItemLogsService;
    /**
     * 查询执行单列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalTreatOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(HospitalTreatOrderHParam hospitalTreatOrder)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalTreatOrder.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalTreatOrderListHVO> list = hospitalTreatOrderService.selectHospitalTreatOrderListHVO(hospitalTreatOrder);
        return getDataTable(list);
    }

    /**
     * 导出执行单列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalTreatOrder:export')")
    @Log(title = "治疗单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HospitalTreatOrder hospitalTreatOrder)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalTreatOrder.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalTreatOrder> list = hospitalTreatOrderService.selectHospitalTreatOrderList(hospitalTreatOrder);
        ExcelUtil<HospitalTreatOrder> util = new ExcelUtil<HospitalTreatOrder>(HospitalTreatOrder.class);
        return util.exportExcel(list, "治疗单数据");
    }

    /**
     * 获取治疗单详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalTreatOrder:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        HospitalTreatOrderLogs hospitalTreatOrderLogs = new HospitalTreatOrderLogs();
        hospitalTreatOrderLogs.setOrderId(orderId);
        List<HospitalTreatOrderLogsHVO> list = hospitalTreatOrderLogsService.selectHospitalTreatOrderLogsListVO(hospitalTreatOrderLogs);
        List<HospitalOrderItemLogs> hospitalOrderItemLogs = iHospitalOrderItemLogsService.selectHospitalOrderItemLogsList(orderId);
        HospitalTreatOrderHVO hospitalTreatOrder = hospitalTreatOrderService.selectHospitalTreatOrderHVOByOrderId(orderId);
        List<HospitalOrderItemListHVO> orderItemListHVOS = hospitalTreatOrderService.selectOrderItemByTreatId(orderId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("logs",list);
        map.put("order",hospitalTreatOrder);
        map.put("orderItems",orderItemListHVOS);
        map.put("itemLogs",hospitalOrderItemLogs);
        return AjaxResult.success(map);
    }

    /**
     * 新增治疗单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalTreatOrder:add')")
    @Log(title = "治疗单", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public R add(@RequestBody HospitalTreatOrder hospitalTreatOrder)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalTreatOrder.setHospitalId( loginUser.getHospital().getHospitalId());
        return hospitalTreatOrderService.insertHospitalTreatOrder(hospitalTreatOrder);
    }

    @Log(title = "完成开单", businessType = BusinessType.INSERT)
    @PostMapping("finishTreatOrder")
    public R finishTreatOrder(@RequestBody HospitalTreatOrderAddParam param)
    {
        return hospitalTreatOrderService.finishTreatOrder(param);
    }

    /**
     * 修改治疗单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalTreatOrder:edit')")
    @Log(title = "治疗单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalTreatOrder hospitalTreatOrder)
    {
        return toAjax(hospitalTreatOrderService.updateHospitalTreatOrder(hospitalTreatOrder));
    }

    /**
     * 删除治疗单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalTreatOrder:remove')")
    @Log(title = "治疗单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(hospitalTreatOrderService.deleteHospitalTreatOrderByOrderIds(orderIds));
    }

    @Log(title = "执行", businessType = BusinessType.UPDATE)
    @PostMapping("/execute")
    public R itemExecute(@RequestBody HospitalTreatOrderExecuteParam param)
    {
       return hospitalTreatOrderService.itemExecute(param);
    }

    /**
     *  查询要执行的订单
     */
    @GetMapping("/getExecuteList")
    public R getExecuteList(@RequestParam("orderId") Long orderId)
    {
        List<HospitalOrderItemListHVO> orderItemListHVOS = hospitalTreatOrderService.selectOrderItemByExecute(orderId);
        return R.ok().put("data",orderItemListHVOS);
    }

}
