package com.zfire.mall.manager.logic.manage.material;

import cn.hutool.core.util.EnumUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.manage.material.MachineConfigureRecordBean;
import com.zfire.mall.manager.enums.MachineConfigureTypeEnum;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.plus.entity.MachineConfigureRecord;
import com.zfire.mall.manager.plus.service.MachineConfigureRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MachineRecordLogic {

    @Resource
    MachineConfigureRecordService machineConfigureRecordService;
    @Resource
    CommonLogic commonLogic;

    public IPage<MachineConfigureRecord> list(String type, String name, String model, String number, BigDecimal volume, Integer pageNum, Integer pageSize) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        return machineConfigureRecordService.lambdaQuery()
                .eq(MachineConfigureRecord::getAdminCompanyId, adminUser.getAdminCompanyId())
                .eq(StringUtils.isNotEmpty(type), MachineConfigureRecord::getType, type)
                .like(StringUtils.isNotEmpty(name), MachineConfigureRecord::getName, name)
                .like(StringUtils.isNotEmpty(model), MachineConfigureRecord::getModel, model)
                .like(StringUtils.isNotEmpty(number), MachineConfigureRecord::getNumber, number)
                .like(Objects.nonNull(volume), MachineConfigureRecord::getVolume, volume)
                .orderByDesc(MachineConfigureRecord::getCreateTime)
                .page(new Page<>(pageNum, pageSize));
    }


    public ExcelData export(List<MachineConfigureRecord> records) {
        String[] titles = new String[]{"类型","名称","型号","编码","体积"};
        List<List<Object>> rows = new ArrayList<>();
        Map<String, Object> enumMap = EnumUtil.getNameFieldMap(MachineConfigureTypeEnum.class, "value");
        for (MachineConfigureRecord record : records) {
            List<Object> row = new ArrayList<>();
            row.add(enumMap.get(record.getType()));
            row.add(record.getName());
            row.add(record.getModel());
            row.add(record.getNumber());
            row.add(record.getVolume());
            rows.add(row);
        }
        ExcelData excelData = new ExcelData();
        excelData.setRows(rows);
        excelData.setTitles(Arrays.asList(titles));
        return excelData;
    }


    public MachineConfigureRecord detail(String id) {
        return machineConfigureRecordService.getById(id);
    }


    public void add(MachineConfigureRecordBean machineConfigureRecordBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        machineConfigureRecordBean.setAdminCompanyId(adminUser.getAdminCompanyId());
        MachineConfigureRecord record = new MachineConfigureRecord();
        BeanUtils.copyProperties(machineConfigureRecordBean, record);
        record.setCreateName(adminUser.getNickName());
        record.setUpdateName(adminUser.getNickName());

        record.insert();
    }

    public void edit(MachineConfigureRecordBean machineConfigureRecordBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        machineConfigureRecordBean.setAdminCompanyId(adminUser.getAdminCompanyId());
        MachineConfigureRecord record = new MachineConfigureRecord();
        BeanUtils.copyProperties(machineConfigureRecordBean, record);
        record.setUpdateName(adminUser.getNickName());

        record.updateById();

        commonLogic.saveLog(machineConfigureRecordBean.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    public void del(List<String> ids) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        machineConfigureRecordService.lambdaUpdate().in(MachineConfigureRecord::getAdminCompanyId, adminUser.getAdminCompanyIds())
                .in(MachineConfigureRecord::getId, ids.stream().distinct().collect(Collectors.toList()))
                .remove();
    }

}
