package com.fs.hospital.controller.hospital;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.domain.HospitalProductPurchaseOrderItem;
import com.fs.hospital.param.HospitalProductPurchaseOrderAddEditParam;
import com.fs.hospital.param.HospitalProductPurchaseOrderParam;
import com.fs.hospital.service.IHospitalProductPurchaseOrderItemService;
import com.fs.hospital.vo.HospitalProductPurchaseOrderHVO;
import com.fs.hospital.vo.HospitalProductPurchaseOrderItemHVO;
import com.fs.hospital.vo.HospitalProductPurchaseOrdeHVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.HospitalProductPurchaseOrder;
import com.fs.hospital.service.IHospitalProductPurchaseOrderService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 采购单Controller
 *
 * @author fs
 * @date 2024-03-12
 */
@RestController
@RequestMapping("/hospital/hospitalPurchaseOrder")
public class HospitalProductPurchaseOrderController extends BaseController
{
    @Autowired
    private IHospitalProductPurchaseOrderService hospitalPurchaseOrderService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IHospitalProductPurchaseOrderItemService hospitalPurchaseOrderItemService;

    /**
     * 查询采购单列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPurchaseOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(HospitalProductPurchaseOrderParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalProductPurchaseOrdeHVO> list = hospitalPurchaseOrderService.selectHospitalPurchaseOrderListPVO(param);
        return getDataTable(list);
    }

    /**
     * 导出采购单列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPurchaseOrder:export')")
    @Log(title = "采购单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HospitalProductPurchaseOrder hospitalProductPurchaseOrder)
    {
        List<HospitalProductPurchaseOrder> list = hospitalPurchaseOrderService.selectHospitalPurchaseOrderList(hospitalProductPurchaseOrder);
        ExcelUtil<HospitalProductPurchaseOrder> util = new ExcelUtil<HospitalProductPurchaseOrder>(HospitalProductPurchaseOrder.class);
        return util.exportExcel(list, "采购单数据");
    }

    /**
     * 获取采购单详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPurchaseOrder:query')")
    @GetMapping(value = "/{orderId}")
    public R getInfo(@PathVariable("orderId") Long orderId)
    {
        HospitalProductPurchaseOrderHVO order = hospitalPurchaseOrderService.selectHospitalPurchaseOrderHVOByOrderId(orderId);
        HospitalProductPurchaseOrderItem item = new HospitalProductPurchaseOrderItem();
        item.setOrderId(orderId);
        List<HospitalProductPurchaseOrderItemHVO> items = hospitalPurchaseOrderItemService.selectHospitalPurchaseOrderItemListPVO(item);
        Map<String,Object> maps=new HashMap<>();
        maps.put("order",order);
        maps.put("items",items);
        return R.ok().put("data",maps);
    }

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

    /**
     * 新增退货出库
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPurchaseOrder:addSales')")
    @PostMapping("/addSales")
    public R addSales(@Validated @RequestBody HospitalProductPurchaseOrderAddEditParam param)
    {
        if (param.getItemList()==null||param.getItemList().size()==0){
            return R.error("退货商品不能为空");
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setHospitalId( loginUser.getHospital().getHospitalId());
        param.setUserId(loginUser.getUser().getUserId());
        param.setStatus(1);
        return hospitalPurchaseOrderService.addSalesOut(param);
    }



    /**
     * 修改采购单
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPurchaseOrder:edit')")
    @Log(title = "采购单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalProductPurchaseOrder hospitalProductPurchaseOrder)
    {
        return toAjax(hospitalPurchaseOrderService.updateHospitalPurchaseOrder(hospitalProductPurchaseOrder));
    }

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