package cn.iocoder.yudao.module.system.service.biz;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MedicineInventoryListReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MedicineInventoryPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.MedicineInventoryDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.MedicineInventoryMapper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 药品库存数量表 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class MedicineInventoryServiceImpl implements MedicineInventoryService {

    @Resource
    private MedicineInventoryMapper medicineInventoryMapper;

    @Override
    public Long createInventory(MedicineInventoryDO inventory) {
        inventory.setCreatedAt(LocalDateTime.now());
        inventory.setUpdatedAt(LocalDateTime.now());
        medicineInventoryMapper.insert(inventory);
        return inventory.getId();
    }

    @Override
    public void updateInventory(MedicineInventoryDO inventory) {
        inventory.setUpdatedAt(LocalDateTime.now());
        medicineInventoryMapper.updateById(inventory);
    }

    @Override
    public MedicineInventoryDO getInventory(Long id) {
        return medicineInventoryMapper.selectById(id);
    }

    @Override
    public MedicineInventoryDO getInventoryByBarcode(String barcode) {
        return medicineInventoryMapper.selectByBarcode(barcode);
    }

    @Override
    public List<MedicineInventoryDO> getInventoriesByBatchNumber(String batchNumber) {
        return medicineInventoryMapper.selectListByBatchNumber(batchNumber);
    }

    @Override
    public List<MedicineInventoryDO> getInventoriesByStatus(Integer inventoryStatus) {
        return medicineInventoryMapper.selectListByInventoryStatus(inventoryStatus);
    }

    @Override
    public JSONArray getInventoriesByTenantId(Long tenantId) {
        List<MedicineInventoryDO> medicineInventoryDOS = medicineInventoryMapper.selectListByTenantId(tenantId);
        // 散装
        List<MedicineInventoryDO> bulkDrug = medicineInventoryDOS.stream().filter(e -> e.getType() == 2).collect(Collectors.toList());
        // 普通
        Map<String, List<MedicineInventoryDO>> commonDrug = medicineInventoryDOS.stream().filter(e -> e.getType() == 1 && StringUtils.isNotBlank(e.getDrugName())).collect(Collectors.groupingBy(MedicineInventoryDO::getDrugName));
        JSONArray bulkDrugArray = JSON.parseArray(JSON.toJSONString(bulkDrug));
        for (int i = 0; i < bulkDrugArray.size(); i++) {
            JSONObject jsonObject = bulkDrugArray.getJSONObject(i);
            jsonObject.put("createdAt",jsonObject.getString("createdAt").replace("T"," "));
            jsonObject.put("updatedAt",jsonObject.getString("updatedAt").replace("T"," "));
        }
        JSONObject commonDrugObject = JSON.parseObject(JSON.toJSONString(commonDrug));
        for (Map.Entry<String, Object> entry : commonDrugObject.entrySet()) {
            JSONArray array = (JSONArray)entry.getValue();
            for (int i = 0; i < array.size(); i++) {
                JSONObject jsonObject = array.getJSONObject(i);
                jsonObject.put("createdAt",jsonObject.getString("createdAt").replace("T"," "));
                jsonObject.put("updatedAt",jsonObject.getString("updatedAt").replace("T"," "));
            }
        }
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(bulkDrugArray);
        jsonArray.add(commonDrugObject);
        return jsonArray;
    }

    @Override
    public List<MedicineInventoryDO> getInventoriesByDrugName(String drugName) {
        return medicineInventoryMapper.selectListByDrugName(drugName);
    }

    @Override
    public void deleteInventory(Long id) {
        medicineInventoryMapper.deleteById(id);
    }

    @Override
    public void deleteInventories(Long[] ids) {
        medicineInventoryMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public PageResult<MedicineInventoryDO> getInventoryPage(MedicineInventoryPageReqVO pageReqVO) {
        return medicineInventoryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MedicineInventoryDO> getInventoryList(MedicineInventoryListReqVO reqVO) {
        return medicineInventoryMapper.selectList(reqVO);
    }
}

