package com.ruoyi.inventory.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.inventory.domain.HealthcareDrugArchive;
import com.ruoyi.inventory.domain.HealthcareDrugArchiveExportVo;
import com.ruoyi.inventory.domain.HealthcareDrugFiling;
import com.ruoyi.inventory.domain.charVo;
import com.ruoyi.inventory.service.IHealthcareDrugArchiveService;
import com.ruoyi.inventory.service.IHealthcareDrugFilingService;

/**
 * 药品档案Controller
 *
 * <<<<<<< HEAD
 * 
 * @author 蒋松霖
 *         =======
 * @author ruoyi
 *         >>>>>>> cf33b39dcb79314a02d35b6116058d8b46856eeb
 * @date 2024-09-19
 */
@RestController
@RequestMapping("/healthcare/archive")
public class HealthcareDrugArchiveController extends BaseController {
    @Autowired
    private IHealthcareDrugArchiveService healthcareDrugArchiveService;

    @Autowired
    private IHealthcareDrugFilingService healthcareDrugFilingService;

    /**
     * 统计库房药品 药品金额 临期药品 库存预警
     *
     */
    @GetMapping("/charMedicine")
    public AjaxResult charMedicine(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        charVo data = healthcareDrugArchiveService.selectcharMedicine(startDate, endDate);
        return AjaxResult.success(data);
    }

    /**
     * 查询药品档案列表
     */
    @PreAuthorize("@ss.hasPermi('healthcare:archive:list')")
    @GetMapping("/list")
    public TableDataInfo list(HealthcareDrugArchive healthcareDrugArchive, String type) {
        startPage();
        if (type != null && !type.isEmpty()) {
            switch (type) {
                case "adevnt":
                    List<HealthcareDrugArchive> list = healthcareDrugArchiveService
                            .selectHealthcareDrugArchiveAdevntList(healthcareDrugArchive);
                    return getDataTable(list);
                case "alerts":
                    List<HealthcareDrugArchive> list2 = healthcareDrugArchiveService
                            .selectHealthcareDrugArchiveAertsList(healthcareDrugArchive);
                    return getDataTable(list2);
            }
        }
        List<HealthcareDrugArchive> list = healthcareDrugArchiveService
                .selectHealthcareDrugArchiveList(healthcareDrugArchive);
        return getDataTable(list);
    }

    /**
     * 导出药品档案列表（含批次明细）
     */
    @PreAuthorize("@ss.hasPermi('healthcare:archive:export')")
    @Log(title = "药品档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HealthcareDrugArchive healthcareDrugArchive) {
        List<HealthcareDrugArchive> list = healthcareDrugArchiveService
                .selectHealthcareDrugArchiveList(healthcareDrugArchive);
        List<HealthcareDrugArchiveExportVo> exportList = new ArrayList<>();
        for (HealthcareDrugArchive archive : list) {
            List<HealthcareDrugFiling> filings = archive.getDrugFilings();
            if (filings != null && !filings.isEmpty()) {
                for (HealthcareDrugFiling filing : filings) {
                    HealthcareDrugArchiveExportVo vo = new HealthcareDrugArchiveExportVo();
                    vo.setMedicineName(archive.getMedicineName());
                    vo.setManufacturer(archive.getManufacturer());
                    vo.setBarcode(archive.getBarcode());
                    vo.setIsSplit("Y".equals(archive.getIsSplit()) ? "是" : "否");
                    vo.setSpecification(archive.getSpecification());
                    vo.setMinNumber(archive.getMinNumber());
                    vo.setBatch(filing.getBatch());
                    vo.setExpiryDate(filing.getExpiryDate());
                    vo.setStoreroomName(filing.getStoreroomName());
                    vo.setBoxCount(filing.getBoxCount());
                    vo.setRetailPrice(filing.getRetailPrice());
                    vo.setMinPrice(filing.getMinPrice());
                    exportList.add(vo);
                }
            } else {
                // 没有批次信息也导出档案基本信息
                HealthcareDrugArchiveExportVo vo = new HealthcareDrugArchiveExportVo();
                vo.setMedicineName(archive.getMedicineName());
                vo.setManufacturer(archive.getManufacturer());
                vo.setBarcode(archive.getBarcode());
                vo.setIsSplit("Y".equals(archive.getIsSplit()) ? "是" : "否");
                vo.setSpecification(archive.getSpecification());
                vo.setMinNumber(archive.getMinNumber());
                exportList.add(vo);
            }
        }
        ExcelUtil<HealthcareDrugArchiveExportVo> util = new ExcelUtil<HealthcareDrugArchiveExportVo>(
                HealthcareDrugArchiveExportVo.class);
        util.exportExcel(response, exportList, "药品档案数据");
    }

    /**
     * 获取药品档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('healthcare:archive:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(healthcareDrugArchiveService.selectHealthcareDrugArchiveById(id));
    }

    /**
     * 新增药品档案
     */
    @PreAuthorize("@ss.hasPermi('healthcare:archive:add')")
    @Log(title = "药品档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HealthcareDrugArchive healthcareDrugArchive) {
        return toAjax(healthcareDrugArchiveService.insertHealthcareDrugArchive(healthcareDrugArchive));
    }

    /**
     * 修改药品档案
     */
    @PreAuthorize("@ss.hasPermi('healthcare:archive:edit')")
    @Log(title = "药品档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HealthcareDrugArchive healthcareDrugArchive) {
        return toAjax(healthcareDrugArchiveService.updateHealthcareDrugArchive(healthcareDrugArchive));
    }

    /**
     * 删除药品档案
     */
    @PreAuthorize("@ss.hasPermi('healthcare:archive:remove')")
    @Log(title = "药品档案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(healthcareDrugArchiveService.deleteHealthcareDrugArchiveByIds(ids));
    }

    /**
     * 导入Excel药品档案
     */
    @PreAuthorize("@ss.hasPermi('healthcare:archive:add')")
    @Log(title = "药品档案", businessType = BusinessType.IMPORT)
    @PostMapping("/archiveExcel")
    public AjaxResult excelImport(MultipartFile file) throws Exception {
        ExcelUtil<HealthcareDrugArchive> Util = new ExcelUtil<>(HealthcareDrugArchive.class);
        List<HealthcareDrugArchive> archiveList = Util.importExcel(file.getInputStream());
        return toAjax(healthcareDrugArchiveService.insertHealthcareDrugArchives(archiveList));
    }

}
