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.HospitalProductOutOrderAddParam;
import com.fs.hospital.vo.HospitalProductOutOrderListHVO;
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.HospitalProductOutOrder;
import com.fs.hospital.service.IHospitalProductOutOrderService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 领用单Controller
 *
 * @author fs
 * @date 2024-03-14
 */
@RestController
@RequestMapping("/hospital/hospitalProductOutOrder")
public class HospitalProductOutOrderController extends BaseController
{
    @Autowired
    private IHospitalProductOutOrderService hospitalProductOutOrderService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询领用单列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProductOutOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(HospitalProductOutOrder hospitalProductOutOrder)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalProductOutOrder.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalProductOutOrderListHVO> list = hospitalProductOutOrderService.selectHospitalProductOutOrderListPVO(hospitalProductOutOrder);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('hospital:hospitalProductOutOrder:list')")
    @GetMapping("/outList")
    public TableDataInfo outList(HospitalProductOutOrder hospitalProductOutOrder)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalProductOutOrder.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalProductOutOrderListHVO> list = hospitalProductOutOrderService.selectHospitalProductOutOrderOutListPVO(hospitalProductOutOrder);
        return getDataTable(list);
    }

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

    /**
     * 获取领用单详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProductOutOrder:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return AjaxResult.success(hospitalProductOutOrderService.selectHospitalProductOutOrderByOrderIdHVO(orderId));
    }

    /**
     * 新增领用单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProductOutOrder:add')")
    @Log(title = "领用单", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody HospitalProductOutOrderAddParam 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 hospitalProductOutOrderService.insertHospitalProductOutOrder(param);
    }

    @PreAuthorize("@ss.hasPermi('hospital:hospitalProductOutOrder:returnItem')")
    @Log(title = "领用单", businessType = BusinessType.INSERT)
    @PostMapping("/returnItem")
    public R returnItem(@RequestBody HospitalProductOutOrderAddParam 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());
        System.out.println(param);
        return hospitalProductOutOrderService.insertReturnHospitalProductOutOrder(param);
    }


    /**
     * 修改领用单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProductOutOrder:edit')")
    @Log(title = "领用单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalProductOutOrder hospitalProductOutOrder)
    {
        return toAjax(hospitalProductOutOrderService.updateHospitalProductOutOrder(hospitalProductOutOrder));
    }

    /**
     * 删除领用单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalProductOutOrder:remove')")
    @Log(title = "领用单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(hospitalProductOutOrderService.deleteHospitalProductOutOrderByOrderIds(orderIds));
    }
}
