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.his.domain.FsStore;
import com.fs.his.service.IFsStoreService;
import com.fs.hospital.param.HospitalStoreParam;
import com.fs.hospital.vo.HospitalStoreHVO;
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.HospitalStore;
import com.fs.hospital.service.IHospitalStoreService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 诊所供应商关联Controller
 *
 * @author fs
 * @date 2024-03-12
 */
@RestController
@RequestMapping("/hospital/hospitalStore")
public class HospitalStoreController extends BaseController
{
    @Autowired
    private IHospitalStoreService hospitalStoreService;
    @Autowired
    private IFsStoreService storeService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询诊所供应商关联列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalStore:list')")
    @GetMapping("/list")
    public TableDataInfo list(HospitalStoreParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalStoreHVO> list = hospitalStoreService.selectHospitalStoreListPVO(param);
        return getDataTable(list);
    }

    /**
     * 导出诊所供应商关联列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalStore:export')")
    @Log(title = "诊所供应商关联", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HospitalStore hospitalStore)
    {
        List<HospitalStore> list = hospitalStoreService.selectHospitalStoreList(hospitalStore);
        ExcelUtil<HospitalStore> util = new ExcelUtil<HospitalStore>(HospitalStore.class);
        return util.exportExcel(list, "诊所供应商关联数据");
    }

    /**
     * 获取诊所供应商关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalStore:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(hospitalStoreService.selectHospitalStoreById(id));
    }

    /**
     * 新增诊所供应商关联
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalStore:add')")
    @Log(title = "诊所供应商关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HospitalStore hospitalStore)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalStore.setHospitalId( loginUser.getHospital().getHospitalId());
        return toAjax(hospitalStoreService.insertHospitalStore(hospitalStore));
    }

    /**
     * 修改诊所供应商关联
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalStore:edit')")
    @Log(title = "诊所供应商关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalStore hospitalStore)
    {
        return toAjax(hospitalStoreService.updateHospitalStore(hospitalStore));
    }

    /**
     * 删除诊所供应商关联
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalStore:remove')")
    @Log(title = "诊所供应商关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hospitalStoreService.deleteHospitalStoreByIds(ids));
    }

    @GetMapping("/getStoreList")
    public R getStoreList(HttpServletRequest request){
        FsStore map=new FsStore();
        map.setStatus(1);
        map.setIsAudit(1);
        List<FsStore> stores=storeService.selectFsStoreList(map);
        return R.ok().put("data", stores);
    }

    @GetMapping("/getHospitalStoreList")
    public R getHospitalStoreList(HttpServletRequest request){
        HospitalStoreParam map=new HospitalStoreParam();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        map.setHospitalId( loginUser.getHospital().getHospitalId());
        map.setStatus(1);
        List<HospitalStoreHVO> list = hospitalStoreService.selectHospitalStoreListPVO(map);
        return R.ok().put("data", list);
    }
}
