package com.fs.hospital.controller.hospital;

import java.util.List;

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 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.hospital.domain.HospitalPayment;
import com.fs.hospital.service.IHospitalPaymentService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 支付明细Controller
 *
 * @author fs
 * @date 2024-03-26
 */
@RestController
@RequestMapping("/hospital/hospitalPayment")
public class HospitalPaymentController extends BaseController
{
    @Autowired
    private IHospitalPaymentService hospitalPaymentService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询支付明细列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPayment:list')")
    @GetMapping("/list")
    public TableDataInfo list(HospitalPayment hospitalPayment)
    {
        startPage();
        List<HospitalPayment> list = hospitalPaymentService.selectHospitalPaymentList(hospitalPayment);
        return getDataTable(list);
    }

    /**
     * 导出支付明细列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPayment:export')")
    @Log(title = "支付明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HospitalPayment hospitalPayment)
    {
        List<HospitalPayment> list = hospitalPaymentService.selectHospitalPaymentList(hospitalPayment);
        ExcelUtil<HospitalPayment> util = new ExcelUtil<HospitalPayment>(HospitalPayment.class);
        return util.exportExcel(list, "支付明细数据");
    }

    /**
     * 获取支付明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPayment:query')")
    @GetMapping(value = "/{paymentId}")
    public AjaxResult getInfo(@PathVariable("paymentId") Long paymentId)
    {
        return AjaxResult.success(hospitalPaymentService.selectHospitalPaymentByPaymentId(paymentId));
    }

    /**
     * 新增支付明细
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPayment:add')")
    @Log(title = "支付明细", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody HospitalPayment hospitalPayment)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalPayment.setHospitalId( loginUser.getHospital().getHospitalId());
        hospitalPayment.setUserId(loginUser.getUser().getUserId());
        return hospitalPaymentService.insertHospitalPayment(hospitalPayment);
    }

    /**
     * 修改支付明细
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPayment:edit')")
    @Log(title = "支付明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalPayment hospitalPayment)
    {
        return toAjax(hospitalPaymentService.updateHospitalPayment(hospitalPayment));
    }

    /**
     * 删除支付明细
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPayment:remove')")
    @Log(title = "支付明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{paymentIds}")
    public AjaxResult remove(@PathVariable Long[] paymentIds)
    {
        return toAjax(hospitalPaymentService.deleteHospitalPaymentByPaymentIds(paymentIds));
    }

    @Log(title = "完成收费", businessType = BusinessType.UPDATE)
    @PostMapping("/finishPayment")
    public R finishPayment(@RequestBody HospitalPayment hospitalPayment)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalPayment.setUserId(loginUser.getUser().getUserId());
        return hospitalPaymentService.finishPayment(hospitalPayment);
    }
}
