package com.xinelu.manage.controller.nursestationconsumable;

import com.xinelu.common.annotation.Log;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.custominterface.Insert;
import com.xinelu.common.custominterface.Update;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.nursestationconsumable.NurseStationConsumable;
import com.xinelu.manage.dto.nursestationconsumable.NurseStationConsumableDTO;
import com.xinelu.manage.dto.nursestationconsumable.NurseStationConsumableImportlDTO;
import com.xinelu.manage.service.nursestationconsumable.INurseStationConsumableService;
import com.xinelu.manage.vo.nursestationconsumable.NurseStationConsumableVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 护理站耗材信息Controller
 *
 * @author 纪寒
 * @date 2022-09-05
 */
@RestController
@RequestMapping("/system/stationConsumable")
public class NurseStationConsumableController extends BaseController {
    @Resource
    private INurseStationConsumableService nurseStationConsumableService;

    /**
     * 查询护理站耗材信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:stationConsumable:list')")
    @GetMapping("/list")
    public TableDataInfo list(NurseStationConsumable nurseStationConsumable) {
        startPage();
        List<NurseStationConsumableVO> list = nurseStationConsumableService.selectNurseStationConsumableList(nurseStationConsumable);
        return getDataTable(list);
    }

    /**
     * 导出护理站耗材信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:stationConsumable:export')")
    @Log(title = "护理站耗材信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NurseStationConsumable nurseStationConsumable) {
        List<NurseStationConsumableVO> list = nurseStationConsumableService.selectNurseStationConsumableList(nurseStationConsumable);
        ExcelUtil<NurseStationConsumableVO> util = new ExcelUtil<>(NurseStationConsumableVO.class);
        util.exportExcel(response, list, "护理站耗材信息数据");
    }

    /**
     * 导入护理站耗材信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:stationConsumable:importStationConsumable')")
    @Log(title = "导入护理站耗材信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        //判断excel里面是否有数据/文件格式
        if (Objects.isNull(file) || StringUtils.isBlank(file.getOriginalFilename())) {
            return AjaxResult.error("请选择所需要上传的护理耗材文件信息！");
        }
        // 获取文件名
        String orgName = file.getOriginalFilename();
        if (!orgName.endsWith(Constants.XLSX) && !orgName.endsWith(Constants.XLS)) {
            return AjaxResult.error("导入文件格式不正确，请导入xlsx或xls格式的文件！");
        }
        ExcelUtil<NurseStationConsumableImportlDTO> util = new ExcelUtil<>(NurseStationConsumableImportlDTO.class);
        List<NurseStationConsumableImportlDTO> list = util.importExcel(file.getInputStream());
        return nurseStationConsumableService.importData(list);
    }

    /**
     * 获取护理站耗材信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:stationConsumable:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(nurseStationConsumableService.selectNurseStationConsumableById(id));
    }

    /**
     * 新增护理站耗材信息
     */
    @PreAuthorize("@ss.hasPermi('system:stationConsumable:add')")
    @Log(title = "护理站耗材信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated(Insert.class) @RequestBody NurseStationConsumableDTO nurseStationConsumableDTO) throws Exception {
        //校验耗材包价格是为负数
        if (CollectionUtils.isEmpty(nurseStationConsumableDTO.getNurseStationConsumables())) {
            return AjaxResult.error("请添加护理站耗材信息！");
        }
        NurseStationConsumable nurseStationConsumable = nurseStationConsumableDTO.getNurseStationConsumables().stream().filter(item -> Objects.nonNull(item.getConsumablePrice()) && item.getConsumablePrice().compareTo(BigDecimal.ZERO) < 0).findFirst().orElse(null);
        if (Objects.nonNull(nurseStationConsumable)) {
            return AjaxResult.error("耗材包价格不能为负数，请重新输入！");
        }
        return nurseStationConsumableService.insertNurseStationConsumable(nurseStationConsumableDTO.getNurseStationConsumables());
    }

    /**
     * 修改护理站耗材信息
     */
    @PreAuthorize("@ss.hasPermi('system:stationConsumable:edit')")
    @Log(title = "护理站耗材信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated(Update.class) @RequestBody NurseStationConsumable nurseStationConsumable) {
        //校验护理站耗材价格
        if (Objects.nonNull(nurseStationConsumable.getConsumablePrice()) && nurseStationConsumable.getConsumablePrice().compareTo(BigDecimal.ZERO) < 0) {
            return AjaxResult.error("耗材包价格不能为负数，请重新输入！");
        }
        return toAjax(nurseStationConsumableService.updateNurseStationConsumable(nurseStationConsumable));
    }

    /**
     * 删除护理站耗材信息
     */
    @PreAuthorize("@ss.hasPermi('system:stationConsumable:remove')")
    @Log(title = "护理站耗材信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(nurseStationConsumableService.deleteNurseStationConsumableByIds(ids));
    }

    /**
     * 根据服务站id查询护理站耗材信息列表(无分页)
     *
     * @param stationId 护理站id
     * @return 护理站列表信息
     */
    @PreAuthorize("@ss.hasPermi('system:stationConsumable:list')")
    @GetMapping("/getListByStationId")
    public AjaxResult getListByStationId(@RequestParam("stationId") Long stationId) {
        NurseStationConsumable nurseStationConsumable = new NurseStationConsumable();
        nurseStationConsumable.setNurseStationId(stationId);
        List<NurseStationConsumableVO> list = nurseStationConsumableService.selectNurseStationConsumableList(nurseStationConsumable);
        return AjaxResult.success(list);
    }
}
