package com.zfire.mall.manager.logic.manage.product.price;

import cn.hutool.core.date.DateUtil;
import com.aliyuncs.utils.StringUtils;
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.exception.RemoteServiceException;
import com.zfire.mall.manager.plus.entity.ProductBusinessPrice;
import com.zfire.mall.manager.plus.entity.SaleType;
import com.zfire.mall.manager.plus.service.ProductBusinessPriceService;
import com.zfire.mall.manager.plus.service.SaleTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ProductBusinessPriceLogic {

    @Resource
    ProductBusinessPriceService productBusinessPriceService;

    @Resource
    SaleTypeService saleTypeService;

    public IPage<ProductBusinessPrice> listPage(String materialNumber, String materialName, String saleTypeId,
                                                String specification, Integer pageNum, Integer pageSize) {
        IPage<ProductBusinessPrice> page = productBusinessPriceService.lambdaQuery()
                .like(StringUtils.isNotEmpty(materialNumber), ProductBusinessPrice::getMaterialNumber, materialNumber)
                .like(StringUtils.isNotEmpty(materialName), ProductBusinessPrice::getMaterialName, materialName)
                .eq(StringUtils.isNotEmpty(saleTypeId), ProductBusinessPrice::getSaleTypeId, saleTypeId)
                .eq(StringUtils.isNotEmpty(specification), ProductBusinessPrice::getSpecification, specification)
                .orderByDesc(ProductBusinessPrice::getCreateTime, ProductBusinessPrice::getId)
                .page(new Page<>(pageNum, pageSize));
        return page;

    }

    public void delete(List<String> ids){
        productBusinessPriceService.lambdaUpdate().in(ProductBusinessPrice::getId,ids).remove();
    }

    /**
     *导入商用工程价格数据
     */
    @Transactional
    public void importData(List<Object> rows) throws Exception{
        //删除所有数据
        productBusinessPriceService.lambdaUpdate().gt(ProductBusinessPrice::getId,0).remove();
        List<ProductBusinessPrice> pdpList = new ArrayList<>();
        List<SaleType> saleTypeList = saleTypeService.list();
        for(Object o : rows){
            List<Object> row = (List<Object>) o;
            String productCode = (String) row.get(0);
            String retailPrice = (String) row.get(1);
            String batchPrice = (String) row.get(2);
            String isRevoke = (String) row.get(3);
            String remark = (String) row.get(4);
            String saleTypeName = (String) row.get(5);
            String materialId = (String) row.get(6);
            String materialNumber = (String) row.get(7);
            String materialName = (String) row.get(8);
            String materialOldNumber = (String) row.get(9);
            String specification = (String) row.get(10);
            String unit = (String) row.get(11);
            String discAmount = (String) row.get(12);

            if(StringUtils.isEmpty(productCode)|| StringUtils.isEmpty(saleTypeName)){
                throw new RemoteServiceException("部分厂产品编码/销售类型 不存在");
            }
            if(StringUtils.isEmpty(retailPrice)|| StringUtils.isEmpty(batchPrice)){
                throw new RemoteServiceException("部分批发价/零售价 不存在");
            }

            ProductBusinessPrice pbp = new ProductBusinessPrice();
            for(SaleType saleType:saleTypeList){
                if(saleTypeName.equals(saleType.getSaleName())){
                    pbp.setSaleTypeCode(saleType.getSaleCode());
                    pbp.setSaleTypeId(saleType.getId());
                    break;
                }
            }
            pbp.setSaleTypeName(saleTypeName);
            pbp.setRetailPrice(new BigDecimal(retailPrice));
            pbp.setBatchPrice(new BigDecimal(batchPrice));
            if(isRevoke.equals("启用")){
                pbp.setIsRevoke(true);
            }
            pbp.setRemark(remark);
            pbp.setProductCode(productCode);
            pbp.setMaterialId(materialId);
            pbp.setMaterialNumber(materialNumber);
            pbp.setMaterialName(materialName);
            pbp.setMaterialOldNumber(materialOldNumber);
            pbp.setSpecification(specification);
            pbp.setUnit(unit);

            if(null != discAmount){
                pbp.setDiscAmount(new BigDecimal(discAmount));
            }
            pdpList.add(pbp);
        }
        productBusinessPriceService.saveBatch(pdpList);
    }


    /**
     * 导出商用工程价格数据
     */
    public ExcelData export(List<ProductBusinessPrice> productBusinessPriceList) {
        String[] titles = new String[]{"厂产品编码*",  "零售价格*", "批发价格*",
                "状态*","备注*","销售类型名称*","金蝶物料ID","金蝶物料编码",
                "金蝶物料名称","金蝶物料旧编码","规格型号","基本单位编码","格力折扣价"};
        List<List<Object>> rows = new ArrayList<>();
        for (ProductBusinessPrice productBusinessPrice : productBusinessPriceList) {
            List<Object> row = new ArrayList<>();
            row.add(productBusinessPrice.getProductCode());
            row.add(productBusinessPrice.getRetailPrice());
            row.add(productBusinessPrice.getBatchPrice());
            row.add(productBusinessPrice.getIsRevoke() ? "启用" : "禁用");
            row.add(productBusinessPrice.getRemark());
            row.add(productBusinessPrice.getSaleTypeName());
            row.add(productBusinessPrice.getMaterialId());
            row.add(productBusinessPrice.getMaterialNumber());
            row.add(productBusinessPrice.getMaterialName());
            row.add(productBusinessPrice.getMaterialOldNumber());
            row.add(productBusinessPrice.getSpecification());
            row.add(productBusinessPrice.getUnit());
            row.add(productBusinessPrice.getDiscAmount());
            rows.add(row);
        }
        ExcelData excelData = new ExcelData();
        excelData.setRows(rows);
        excelData.setTitles(Arrays.asList(titles));
        return excelData;
    }

    public ProductBusinessPrice detail(String id){
        ProductBusinessPrice bean = productBusinessPriceService.getById(id);
        return bean;
    }

    public void update(ProductBusinessPrice productBusinessPrice){
        productBusinessPrice.updateById();
    }

}
