package com.fs.hospital.controller.hospital;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.fs.common.utils.ServletUtils;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.hospital.domain.HospitalMember;
import com.fs.hospital.service.IHospitalMemberService;
import com.fs.hospital.vo.HospitalMemberBillHVO;
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.HospitalMemberBill;
import com.fs.hospital.service.IHospitalMemberBillService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 会员帐单Controller
 *
 * @author fs
 * @date 2024-03-04
 */
@RestController
@RequestMapping("/hospital/hospitalMemberBill")
public class HospitalMemberBillController extends BaseController
{
    @Autowired
    private IHospitalMemberBillService hospitalMemberBillService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IHospitalMemberService hospitalMemberService;

    /**
     * 查询会员帐单列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalMemberBill:list')")
    @GetMapping("/list")
    public TableDataInfo list(HospitalMemberBill hospitalMemberBill)
    {
        startPage();
        List<HospitalMemberBillHVO> list = hospitalMemberBillService.selectHospitalMemberBillListPVO(hospitalMemberBill);
        return getDataTable(list);
    }

    /**
     * 导出会员帐单列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalMemberBill:export')")
    @Log(title = "会员帐单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HospitalMemberBill hospitalMemberBill)
    {
        List<HospitalMemberBill> list = hospitalMemberBillService.selectHospitalMemberBillList(hospitalMemberBill);
        ExcelUtil<HospitalMemberBill> util = new ExcelUtil<HospitalMemberBill>(HospitalMemberBill.class);
        return util.exportExcel(list, "会员帐单数据");
    }

    /**
     * 获取会员帐单详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalMemberBill:query')")
    @GetMapping(value = "/{billId}")
    public AjaxResult getInfo(@PathVariable("billId") Long billId)
    {
        return AjaxResult.success(hospitalMemberBillService.selectHospitalMemberBillByBillId(billId));
    }

    /**
     * 新增会员帐单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalMemberBill:add')")
    @Log(title = "会员帐单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HospitalMemberBill hospitalMemberBill)
    {
        return toAjax(hospitalMemberBillService.insertHospitalMemberBill(hospitalMemberBill));
    }

    @Log(title = "会员帐单", businessType = BusinessType.INSERT)
    @PostMapping("/addRefundBill")
    public AjaxResult addRefundBill(@RequestBody HospitalMemberBill hospitalMemberBill)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        HospitalMember hospitalMember = hospitalMemberService.selectHospitalMemberByMemberId(hospitalMemberBill.getMemberId());
        if (hospitalMember!=null){
            if ((hospitalMember.getBalance().compareTo(hospitalMemberBill.getMoney())<0)){
             return AjaxResult.error("余额不足");
            }
            hospitalMember.setBalance(hospitalMember.getBalance().subtract(hospitalMemberBill.getMoney()));
            hospitalMemberBill.setHospitalId(loginUser.getHospital().getHospitalId());
            hospitalMemberService.updateHospitalMember(hospitalMember);
            hospitalMemberBill.setBillType(2);
            hospitalMemberBill.setMoney(hospitalMemberBill.getMoney().multiply(new BigDecimal(-1)));
            hospitalMemberBill.setBalance(hospitalMember.getBalance());
            hospitalMember.setCreateTime(new Date());
            hospitalMemberBill.setTitle("退款扣款");
            return toAjax(hospitalMemberBillService.insertHospitalMemberBill(hospitalMemberBill));
        }else {
            return AjaxResult.error("会员不存在");
        }
    }

    /**
     * 修改会员帐单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalMemberBill:edit')")
    @Log(title = "会员帐单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalMemberBill hospitalMemberBill)
    {
        return toAjax(hospitalMemberBillService.updateHospitalMemberBill(hospitalMemberBill));
    }

    /**
     * 删除会员帐单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalMemberBill:remove')")
    @Log(title = "会员帐单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{billIds}")
    public AjaxResult remove(@PathVariable Long[] billIds)
    {
        return toAjax(hospitalMemberBillService.deleteHospitalMemberBillByBillIds(billIds));
    }
}
