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 com.fs.hospital.param.HospitalProductCheckOrderAddParam;
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.HospitalProductCheckOrder;
import com.fs.hospital.service.IHospitalProductCheckOrderService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 采购单Controller
 *
 * @author fs
 * @date 2024-03-14
 */
@RestController
@RequestMapping("/hospital/hospitalProductCheckOrder")
public class HospitalProductCheckOrderController extends BaseController
{
    @Autowired
    private IHospitalProductCheckOrderService hospitalProductCheckOrderService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询采购单列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProductCheckOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(HospitalProductCheckOrder hospitalProductCheckOrder)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalProductCheckOrder.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalProductCheckOrder> list = hospitalProductCheckOrderService.selectHospitalProductCheckOrderList(hospitalProductCheckOrder);
        return getDataTable(list);
    }

    /**
     * 导出采购单列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProductCheckOrder:export')")
    @Log(title = "采购单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HospitalProductCheckOrder hospitalProductCheckOrder)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalProductCheckOrder.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalProductCheckOrder> list = hospitalProductCheckOrderService.selectHospitalProductCheckOrderList(hospitalProductCheckOrder);
        ExcelUtil<HospitalProductCheckOrder> util = new ExcelUtil<HospitalProductCheckOrder>(HospitalProductCheckOrder.class);
        return util.exportExcel(list, "采购单数据");
    }

    /**
     * 获取采购单详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProductCheckOrder:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return AjaxResult.success(hospitalProductCheckOrderService.selectHospitalProductCheckOrderByOrderIdHVO(orderId));
    }

    /**
     * 新增采购单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProductCheckOrder:add')")
    @Log(title = "采购单", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody HospitalProductCheckOrderAddParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setHospitalId( loginUser.getHospital().getHospitalId());
        if (param.getItemList()==null||param.getItemList().size()==0){
            return R.error("请添加商品");
        }
        param.setHospitalId( loginUser.getHospital().getHospitalId());
        param.setUserId(loginUser.getUser().getUserId());
        param.setStatus(4);
        param.setProductCount(param.getItemList().size());
        return hospitalProductCheckOrderService.insertHospitalProductCheckOrder(param);
    }

    /**
     * 修改采购单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProductCheckOrder:edit')")
    @Log(title = "采购单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalProductCheckOrder hospitalProductCheckOrder)
    {
        return toAjax(hospitalProductCheckOrderService.updateHospitalProductCheckOrder(hospitalProductCheckOrder));
    }

    /**
     * 删除采购单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProductCheckOrder:remove')")
    @Log(title = "采购单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(hospitalProductCheckOrderService.deleteHospitalProductCheckOrderByOrderIds(orderIds));
    }
}
