package cn.lihongjie.coal.warehouseReceipt.service;

import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.base.service.BaseService;
import cn.lihongjie.coal.warehouseGoodsSummary.service.WarehouseGoodsSummaryService;
import cn.lihongjie.coal.warehouseReceipt.dto.CreateWarehouseReceiptDto;
import cn.lihongjie.coal.warehouseReceipt.dto.UpdateWarehouseReceiptDto;
import cn.lihongjie.coal.warehouseReceipt.dto.WarehouseReceiptDto;
import cn.lihongjie.coal.warehouseReceipt.entity.WarehouseReceiptEntity;
import cn.lihongjie.coal.warehouseReceipt.mapper.WarehouseReceiptMapper;
import cn.lihongjie.coal.warehouseReceipt.repository.WarehouseReceiptRepository;
import cn.lihongjie.coal.warehouseReceiptDetail.entity.WarehouseReceiptDetailEntity;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Slf4j
@Transactional
public class WarehouseReceiptService
        extends BaseService<WarehouseReceiptEntity, WarehouseReceiptRepository> {
    @Autowired private WarehouseReceiptRepository repository;

    @Autowired private WarehouseReceiptMapper mapper;

    @Autowired private ConversionService conversionService;

    @Autowired WarehouseGoodsSummaryService warehouseGoodsSummaryService;

    public WarehouseReceiptDto create(CreateWarehouseReceiptDto request) {
        WarehouseReceiptEntity entity = mapper.toEntity(request);

        this.repository.save(entity);

        handleType3Create(entity);
        this.repository.save(entity);
        warehouseGoodsSummaryService.refresh(List.of(entity));
        return getById(entity.getId());
    }

    private void handleType3Create(WarehouseReceiptEntity entity) {

        // 生成入库单
        WarehouseReceiptEntity type0 = new WarehouseReceiptEntity();

        type0.setReceiptType("0");
        type0.setReceiptNo(entity.getReceiptNo() + "-0");
        type0.setReason("调货");

        type0.setReasonDesc("调货产生的入库单");


        type0.setWarehouse(entity.getWarehouse2());

        type0.setReceiptDate(entity.getReceiptDate());

        var details = new ArrayList<WarehouseReceiptDetailEntity>();

        for (WarehouseReceiptDetailEntity warehouseReceiptDetailEntity : entity.getDetail()) {
            WarehouseReceiptDetailEntity detail = new WarehouseReceiptDetailEntity();
            detail.setGoods(warehouseReceiptDetailEntity.getGoods());
            detail.setNumber(warehouseReceiptDetailEntity.getNumber());
            detail.setPrice(warehouseReceiptDetailEntity.getPrice());
            detail.setShelve(warehouseReceiptDetailEntity.getShelve2());
            details.add(detail);
        }

        type0.setDetail(details);

        this.repository.save(type0);

        // 生成出库单


        WarehouseReceiptEntity type1 = new WarehouseReceiptEntity();

        type1.setReceiptType("1");
        type1.setReceiptNo(entity.getReceiptNo() + "-1");
        type1.setReason("调货");

        type1.setReasonDesc("调货产生的出库单");


        type1.setWarehouse(entity.getWarehouse());

        type1.setReceiptDate(entity.getReceiptDate());

        details = new ArrayList<WarehouseReceiptDetailEntity>();

        for (WarehouseReceiptDetailEntity warehouseReceiptDetailEntity : entity.getDetail()) {
            WarehouseReceiptDetailEntity detail = new WarehouseReceiptDetailEntity();
            detail.setGoods(warehouseReceiptDetailEntity.getGoods());
            detail.setNumber(warehouseReceiptDetailEntity.getNumber());
            detail.setPrice(warehouseReceiptDetailEntity.getPrice());
            detail.setShelve(warehouseReceiptDetailEntity.getShelve());
            details.add(detail);
        }

        type1.setDetail(details);

        this.repository.save(type1);

        entity.setReceipt0(type0);
        entity.setReceipt1(type1);




    }

    public WarehouseReceiptDto update(UpdateWarehouseReceiptDto request) {
        WarehouseReceiptEntity entity = this.repository.get(request.getId());
        this.mapper.updateEntity(entity, request);

        handleType3Update(entity);
        this.repository.save(entity);

        warehouseGoodsSummaryService.refresh(List.of(entity));

        return getById(entity.getId());
    }

    private void handleType3Update(WarehouseReceiptEntity entity) {
        repository.delete(entity.getReceipt0());
        repository.delete(entity.getReceipt1());
        handleType3Create(entity);

    }

    public void delete(IdRequest request) {
        List<WarehouseReceiptEntity> allById = this.repository.findAllById(request.getIds());
        this.repository.deleteAllById(request.getIds());
        handleType3Delete(allById);
        warehouseGoodsSummaryService.refresh(allById);
    }

    private void handleType3Delete(List<WarehouseReceiptEntity> allById) {


    }

    public WarehouseReceiptDto getById(String id) {
        WarehouseReceiptEntity entity = repository.get(id);

        return mapper.toDto(entity);
    }

    public Page<WarehouseReceiptDto> list(CommonQuery query) {
        Page<WarehouseReceiptEntity> page =
                repository.findAll(
                        query.specification(conversionService),
                        PageRequest.of(
                                query.getPageNo(),
                                query.getPageSize(),
                                Sort.by(query.getOrders())));

        return page.map(this.mapper::toDto);
    }
}
