package com.fs.hospital.controller.hospital;

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.vo.HospitalProductListHVO;
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.HospitalProduct;
import com.fs.hospital.service.IHospitalProductService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 诊所商品Controller
 *
 * @author fs
 * @date 2024-03-12
 */
@RestController
@RequestMapping("/hospital/hospitalProduct")
public class HospitalProductController extends BaseController
{
    @Autowired
    private IHospitalProductService hospitalProductService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询诊所商品列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(HospitalProduct hospitalProduct)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalProduct.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalProduct> list = hospitalProductService.selectHospitalProductList(hospitalProduct);
        return getDataTable(list);
    }

    /**
     * 导出诊所商品列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProduct:export')")
    @Log(title = "诊所商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HospitalProduct hospitalProduct)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalProduct.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalProduct> list = hospitalProductService.selectHospitalProductList(hospitalProduct);
        ExcelUtil<HospitalProduct> util = new ExcelUtil<HospitalProduct>(HospitalProduct.class);
        return util.exportExcel(list, "诊所商品数据");
    }

    /**
     * 获取诊所商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProduct:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return AjaxResult.success(hospitalProductService.selectHospitalProductByProductId(productId));
    }

    /**
     * 新增诊所商品
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProduct:add')")
    @Log(title = "诊所商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HospitalProduct hospitalProduct)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalProduct.setHospitalId( loginUser.getHospital().getHospitalId());
        return toAjax(hospitalProductService.insertHospitalProduct(hospitalProduct));
    }

    /**
     * 修改诊所商品
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProduct:edit')")
    @Log(title = "诊所商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalProduct hospitalProduct)
    {
        return toAjax(hospitalProductService.updateHospitalProduct(hospitalProduct));
    }

    /**
     * 删除诊所商品
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProduct:remove')")
    @Log(title = "诊所商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(hospitalProductService.deleteHospitalProductByProductIds(productIds));
    }

    @GetMapping("/listByPurchase")
    public TableDataInfo listByPurchase(HospitalProduct hospitalProduct)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalProduct.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalProductListHVO> list = hospitalProductService.selectHospitalProductListPVO(hospitalProduct);
        return getDataTable(list);
    }
}
