package com.fs.hospital.controller.hospital;

import java.util.List;

import com.fs.common.BeanCopyUtils;
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.HospitalOrderCashierParam;
import com.fs.hospital.param.HospitalOrderParam;
import com.fs.hospital.param.HospitalTreatOrderAddParam;
import com.fs.hospital.vo.HospitalOrderListHVO;
import org.json.JSONObject;
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.HospitalOrder;
import com.fs.hospital.service.IHospitalOrderService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 诊疗订单Controller
 *
 * @author fs
 * @date 2024-03-22
 */
@RestController
@RequestMapping("/hospital/hospitalOrder")
public class HospitalOrderController extends BaseController
{
    @Autowired
    private IHospitalOrderService hospitalOrderService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询诊疗订单列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(HospitalOrderParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalOrderListHVO> list = hospitalOrderService.selectHospitalOrderListHVO(param);
        return getDataTable(list);
    }

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

    /**
     * 获取诊疗订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalOrder:query')")
    @GetMapping(value = "/{orderId}")
    public R getInfo(@PathVariable("orderId") Long orderId)
    {
        return hospitalOrderService.selectHospitalOrderByOrderId(orderId);
    }

    /**
     * 新增诊疗订单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalOrder:add')")
    @Log(title = "诊疗订单", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody HospitalOrder hospitalOrder)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalOrder.setHospitalId( loginUser.getHospital().getHospitalId());
        return toAjax(hospitalOrderService.insertHospitalOrder(hospitalOrder));
    }

    /**
     * 修改诊疗订单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalOrder:edit')")
    @Log(title = "诊疗订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalOrder hospitalOrder)
    {
        return toAjax(hospitalOrderService.updateHospitalOrder(hospitalOrder));
    }

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

    @Log(title = "收费", businessType = BusinessType.UPDATE)
    @PostMapping("/cashier")
    public R cashier(@RequestBody HospitalOrderCashierParam param)
    {
        return hospitalOrderService.cashier(param);
    }

    @Log(title = "挂单or挂单or退费等门诊-收费操作",businessType = BusinessType.DELETE)
    @PostMapping("/handleOrderChangeExecute/{orderId}")
    public AjaxResult handleOrderChangeExecute(@PathVariable Long orderId,@RequestBody int type){
        return toAjax(hospitalOrderService.handleOrderChangeExecute(orderId,type));
    }


    /**
     *  获取患者的收费记录
     */
    @GetMapping("/getFeeRecordsByPatientIdList/{patientId}")
    public R selectHospitalOrderByPatientIdList(@PathVariable Long patientId,@RequestParam("status") int status){

        return R.ok().put("data",hospitalOrderService.selectHospitalOrderByPatientIdList(patientId,status));
    }

    /** 获取患者的某一条收费详细记录 */
    @GetMapping("/getFeeRecordsByOrderId/{orderId}")
    public R getFeeRecordsByOrderId(@PathVariable Long orderId){

        return  R.ok().put("data", hospitalOrderService.getFeeRecordsByOrderId(orderId));
    }

    /** 完成新增收费 */
    @PostMapping("finishOrder")
    public R finishOrder(@RequestBody HospitalOrderParam param)
    {
        System.out.println(param);
        return R.ok().put("data", hospitalOrderService.finishOrder(param));
    }

}
