package com.fs.his.controller;

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

import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.SecurityUtils;
import com.fs.his.domain.FsChineseMedicine;
import com.fs.his.domain.FsFollowTemp;
import com.fs.his.param.FsPackageParam;
import com.fs.his.param.FsStoreProductPackageModifyParam;
import com.fs.his.service.IFsFollowTempService;
import com.fs.his.utils.RedisCacheUtil;
import com.fs.his.vo.FsPackageChooseVO;
import com.fs.his.vo.FsPackageExcelVO;
import com.fs.his.vo.FsPackageListVO;
import com.fs.his.vo.OptionsVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.his.domain.FsPackage;
import com.fs.his.service.IFsPackageService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.cache.annotation.Cacheable;

/**
 * 套餐包Controller
 *
 * @author fs
 * @date 2023-08-25
 */
@RestController
@RequestMapping("/his/package")
public class FsPackageController extends BaseController
{
    @Autowired
    private IFsPackageService fsPackageService;
    @Autowired
    private IFsFollowTempService fsFollowTempService;
    /**
     * 查询套餐包列表
     */

    @PreAuthorize("@ss.hasPermi('his:package:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsPackageParam fsPackage)
    {
        startPage();
        List<FsPackageListVO> list = fsPackageService.selectFsPackageListVO(fsPackage);
        return getDataTable(list);
    }

    /**
     * 导出套餐包列表
     */
    @PreAuthorize("@ss.hasPermi('his:package:export')")
    @Log(title = "套餐包", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsPackage fsPackage)
    {
        List<FsPackageExcelVO> list = fsPackageService.selectFsPackageListExcelVO(fsPackage);
        ExcelUtil<FsPackageExcelVO> util = new ExcelUtil<FsPackageExcelVO>(FsPackageExcelVO.class);
        return util.exportExcel(list, "套餐包数据");
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<FsPackage> util = new ExcelUtil<>(FsPackage.class);
        return util.importTemplateExcel("导入模板");
    }

    @Log(title = "导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<FsPackage> util = new ExcelUtil<FsPackage>(FsPackage.class);
        List<FsPackage> list = util.importExcel(file.getInputStream());
        String message = fsPackageService.importData(list);
        return AjaxResult.success(message);
    }

    /**
     * 获取套餐包详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:package:query')")
    @GetMapping(value = "/{packageId}")
    public AjaxResult getInfo(@PathVariable("packageId") Long packageId)
    {
        return AjaxResult.success(fsPackageService.selectFsPackageByPackageIdVO(packageId));
    }


    @Autowired
    RedisCacheUtil redisCacheUtil;
    /**
     * 新增套餐包
     */
    @PreAuthorize("@ss.hasPermi('his:package:add')")
    @Log(title = "套餐包", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsPackage fsPackage)
    {
        fsPackageService.insertFsPackage(fsPackage);
        redisCacheUtil.delRedisKey("getPackageList");
        redisCacheUtil.delRedisKey("getPackageById");
        return toAjax(1);
    }

    /**
     * 修改套餐包
     */
    @PreAuthorize("@ss.hasPermi('his:package:edit')")
    @Log(title = "套餐包", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsPackage fsPackage)
    {
        logger.info("修改套餐包；"+ SecurityUtils.getUsername());
        fsPackageService.updateFsPackage(fsPackage);
        redisCacheUtil.delRedisKey("getPackageList");
        redisCacheUtil.delRedisKey("getPackageById");
        return toAjax(1);
    }

    /**
     * 删除套餐包
     */
    @PreAuthorize("@ss.hasPermi('his:package:remove')")
    @Log(title = "套餐包", businessType = BusinessType.DELETE)
	@DeleteMapping("/{packageIds}")
    public AjaxResult remove(@PathVariable Long[] packageIds)
    {
        return toAjax(fsPackageService.deleteFsPackageByPackageIds(packageIds));
    }


    /**
     * 批量复制套餐包
     */
    @PreAuthorize("@ss.hasPermi('his:package:bulkCopy')")
    @Log(title = "套餐包", businessType = BusinessType.DELETE)
    @GetMapping("/bulkCopy/{packageIds}")
    public AjaxResult bulkCopy(@PathVariable Long[] packageIds)
    {
        return toAjax(fsPackageService.bulkCopyFsPackageByPackage(packageIds));
    }

    /**
     * 查询套餐包列表
     */
    @GetMapping("/allList")
    public TableDataInfo getHospital()
    {
        List<OptionsVO> list = fsPackageService.selectAllPackageList();
        return getDataTable(list);
    }


    @GetMapping("/followTempList")
    public TableDataInfo followTempList(FsFollowTemp fsFollowTemp)
    {
        return getDataTable(fsFollowTempService.selectAllFsFollowTempList());
    }

    @GetMapping("/listBySearch")
    public R listBySearCh(FsPackageParam fsPackage)
    {
        fsPackage.setIsShow(1);
        List<FsPackageListVO> list = fsPackageService.selectFsPackageListVO(fsPackage);
        return R.ok().put("data",list);
    }

    @PreAuthorize("@ss.hasPermi('his:package:edit')")
    @Log(title = "套餐包", businessType = BusinessType.UPDATE)
    @PostMapping("/modifyMore")
    public AjaxResult modifyMore(@RequestBody FsStoreProductPackageModifyParam param) {
        return toAjax(fsPackageService.updatePackagesStatus(param.getPackageIds(),param.getStatus()));
    }

    @GetMapping("/getChoosePackageList")
    public R getChoosePackageList(@RequestParam(required = false) String packageName,
                                  @RequestParam(required = false) String secondName,
                                  @RequestParam(required = false) Integer packageType,
                                  @RequestParam(required = false) Integer packageSubType,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Map<String,Object> params = new HashMap<>();
        params.put("packageName", packageName);
        params.put("secondName", secondName);
        params.put("packageType", packageType);
        params.put("packageSubType", packageSubType);

        PageHelper.startPage(pageNum, pageSize);
        List<FsPackageChooseVO> list = fsPackageService.getChoosePackageListByMap(params);
        return R.ok().put("data", new PageInfo<>(list));
    }

}
