package com.fs.hospital.controller.hospital;

import java.util.*;

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.his.vo.FsInquiryOrderListDVO;
import com.fs.hospital.domain.HospitalOrderItem;
import com.fs.hospital.domain.HospitalPayment;
import com.fs.hospital.param.HospitalOutpatientOrderFinishParam;
import com.fs.hospital.param.HospitalOutpatientOrderParam;
import com.fs.hospital.service.IHospitalOrderItemService;
import com.fs.hospital.service.IHospitalPaymentService;
import com.fs.hospital.vo.HospitalOutpatientOrderCountHVO;
import com.fs.hospital.vo.HospitalOutpatientOrderHVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.HospitalOutpatientOrder;
import com.fs.hospital.service.IHospitalOutpatientOrderService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 门诊单Controller
 *
 * @author fs
 * @date 2024-03-22
 */
@RestController
@RequestMapping("/hospital/hospitalOutpatientOrder")
public class HospitalOutpatientOrderController extends BaseController
{
    @Autowired
    private IHospitalOutpatientOrderService hospitalOutpatientOrderService;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private IHospitalPaymentService paymentService;
    @Autowired
    private IHospitalOrderItemService hospitalOrderItemService;

    /**
     * 查询门诊单列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalOutpatientOrder:list')")
    @GetMapping("/list")
    public R list(HospitalOutpatientOrderParam param)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setHospitalId( loginUser.getHospital().getHospitalId());
        if (param.getYuyueTime()==null){
            param.setYuyueTime(new Date());
        }
        List<HospitalOutpatientOrderHVO> list = hospitalOutpatientOrderService.selectHospitalOutpatientOrderListHVO(param);
        PageInfo<HospitalOutpatientOrderHVO> listPageInfo=new PageInfo<>(list);
        List<HospitalOutpatientOrderCountHVO>  countList = hospitalOutpatientOrderService.selectHospitalOutpatientOrderCountListHVO(param);
        List<HospitalOutpatientOrderCountHVO> allCount = new ArrayList<>();
        HospitalOutpatientOrderCountHVO count = hospitalOutpatientOrderService.selectHospitalOutpatientOrderAllCountByTime(param);
        allCount.add(count);
        return R.ok().put("orderList", listPageInfo).put("countList", countList).put("allCount", allCount);
    }

    @GetMapping("/orderList")
    public R orderList(HospitalOutpatientOrderParam param)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalOutpatientOrderHVO> list = hospitalOutpatientOrderService.selectHospitalOutpatientOrderListHVO(param);
        PageInfo<HospitalOutpatientOrderHVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("row", listPageInfo);
    }

    @GetMapping("/outpatientOrderList")
    public R outpatientOrderList(HospitalOutpatientOrderParam param)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalOutpatientOrderHVO> list = hospitalOutpatientOrderService.selectAllOutpatientOrderListHVO(param);
        PageInfo<HospitalOutpatientOrderHVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("row", listPageInfo);
    }

    /**
     * 导出门诊单列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalOutpatientOrder:export')")
    @Log(title = "门诊单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HospitalOutpatientOrder hospitalOutpatientOrder)
    {
        List<HospitalOutpatientOrder> list = hospitalOutpatientOrderService.selectHospitalOutpatientOrderList(hospitalOutpatientOrder);
        ExcelUtil<HospitalOutpatientOrder> util = new ExcelUtil<HospitalOutpatientOrder>(HospitalOutpatientOrder.class);
        return util.exportExcel(list, "门诊单数据");
    }

    /**
     * 获取挂号详情信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalOutpatientOrder:query')")
    @GetMapping(value = "/{orderId}")
    public R getInfo(@PathVariable("orderId") Long orderId)
    {
        HospitalOutpatientOrder hospitalOutpatientOrder = hospitalOutpatientOrderService.selectHospitalOutpatientOrderByOrderId(orderId);
        if (hospitalOutpatientOrder!=null){
            HospitalOrderItem orderItem = hospitalOrderItemService.selectHospitalOrderItemByRegistration(hospitalOutpatientOrder.getOrderId());
            return R.ok().put("data", hospitalOutpatientOrder).put("orderItem",orderItem);
        }
        return R.error("未查询到挂号详情");
    }

    /**
     * 获取门诊单详情信息
     */
    @GetMapping("/getOutpatientOrder")
    public R getOutPatientOrder(@RequestParam("orderId") Long orderId)
    {
        return hospitalOutpatientOrderService.getOutpatientOrder(orderId);
    }

    /**  获取患者门诊历史订单 */
    @GetMapping("/getOutpatientOrderHistory")
    public R getOutpatientOrderHistory(@RequestParam("orderId") Long orderId){
        return hospitalOutpatientOrderService.getOutpatientOrderHistory(orderId);
    }

    /**
     * 新增挂号预约
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalOutpatientOrder:add')")
    @Log(title = "挂号预约", businessType = BusinessType.INSERT)
    @PostMapping("/registration")
    public R add(@RequestBody HospitalOutpatientOrder hospitalOutpatientOrder)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalOutpatientOrder.setHospitalId( loginUser.getHospital().getHospitalId());
        hospitalOutpatientOrder.setDeptId(loginUser.getUser().getDeptId());
        return hospitalOutpatientOrderService.insertHospitalOutpatientOrder(hospitalOutpatientOrder);
    }

    /**
     * 新增门诊
     */
//    @PreAuthorize("@ss.hasPermi('hospital:hospitalOutpatientOrder:add')")
    @Log(title = "门诊", businessType = BusinessType.INSERT)
    @PostMapping("addOutpatientOrder")
    public R addOutpatientOrder(@RequestBody HospitalOutpatientOrder hospitalOutpatientOrder)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalOutpatientOrder.setHospitalId( loginUser.getHospital().getHospitalId());
        hospitalOutpatientOrder.setDeptId(loginUser.getUser().getDeptId());
        return hospitalOutpatientOrderService.createOutpatientOrder(hospitalOutpatientOrder);
    }

    @Log(title = "完成接诊", businessType = BusinessType.INSERT)
    @PostMapping("finishOutpatientOrder")
    @RepeatSubmit
    public R finishOutpatientOrder(@RequestBody HospitalOutpatientOrderFinishParam param)
    {
        return hospitalOutpatientOrderService.finishOutpatientOrder(param);
    }


    /**
     * 修改门诊单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalOutpatientOrder:edit')")
    @Log(title = "门诊单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalOutpatientOrder hospitalOutpatientOrder)
    {
        return toAjax(hospitalOutpatientOrderService.updateHospitalOutpatientOrder(hospitalOutpatientOrder));
    }

    /**
     * 删除门诊单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalOutpatientOrder:remove')")
    @Log(title = "门诊单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(hospitalOutpatientOrderService.deleteHospitalOutpatientOrderByOrderIds(orderIds));
    }




}
