package com.prolog.eis.service.shelves.impl;

import com.prolog.eis.dao.shelves.DismantlingLocationHitRecordMapper;
import com.prolog.eis.model.shelves.DismantlingLocationHitRecord;
import com.prolog.eis.model.shelves.DismantlingLocationHitRecordHis;
import com.prolog.eis.service.shelves.DismantlingLocationHitRecordHisService;
import com.prolog.eis.service.shelves.DismantlingLocationHitRecordService;
import com.prolog.framework.core.restriction.Criteria;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DismantlingLocationHitRecordServiceImpl implements DismantlingLocationHitRecordService {

    @Resource
    private DismantlingLocationHitRecordMapper dismantlingLocationHitRecordMapper;
    @Resource
    private DismantlingLocationHitRecordHisService dismantlingLocationHitRecordHisService;

    @Override
    public long saveBatch(List<DismantlingLocationHitRecord> dismantlingLocationHitRecords) {
        return dismantlingLocationHitRecordMapper.saveBatch(dismantlingLocationHitRecords);
    }

    @Override
    public long deleteById(Integer id) {
        return dismantlingLocationHitRecordMapper.deleteById(id, DismantlingLocationHitRecord.class);
    }

    @Override
    public List<DismantlingLocationHitRecord> findByCriteria(Criteria criteria) {
        return dismantlingLocationHitRecordMapper.findByCriteria(criteria);
    }

    @Override
    public void toHis(String taskId, String location) {
        Criteria criteria = new Criteria(DismantlingLocationHitRecord.class);
        criteria.restrictionBuilder().eq(DismantlingLocationHitRecord::getPalletNo, location).eq(DismantlingLocationHitRecord::getTaskDetailId, taskId).build();
        List<DismantlingLocationHitRecord> records = this.findByCriteria(criteria);
        if (!records.isEmpty()) {
            DismantlingLocationHitRecord hitRecord = records.get(0);
            DismantlingLocationHitRecordHis recordHis = new DismantlingLocationHitRecordHis();
            BeanUtils.copyProperties(hitRecord, recordHis);
            dismantlingLocationHitRecordHisService.saveBatch(Arrays.asList(recordHis));
            this.deleteById(hitRecord.getId());
        }


    }

    @Override
    public void toHis(String taskId) {
        Criteria criteria = new Criteria(DismantlingLocationHitRecord.class);
        criteria.restrictionBuilder().eq(DismantlingLocationHitRecord::getTaskDetailId, taskId).build();
        List<DismantlingLocationHitRecord> records = this.findByCriteria(criteria);
        if (!records.isEmpty()) {
            DismantlingLocationHitRecord hitRecord = records.get(0);
            DismantlingLocationHitRecordHis recordHis = new DismantlingLocationHitRecordHis();
            BeanUtils.copyProperties(hitRecord, recordHis);
            dismantlingLocationHitRecordHisService.saveBatch(Arrays.asList(recordHis));
            this.deleteById(hitRecord.getId());
        }
    }

    @Override
    public long updateMapByCriteria(Map<String, Object> map, Criteria criteria) {
        return dismantlingLocationHitRecordMapper.updateMapByCriteria(map, criteria);
    }

    @Override
    public void deleteByMap(Map<String, Object> map) {
        dismantlingLocationHitRecordMapper.deleteByMap(map, DismantlingLocationHitRecord.class);
    }

    @Override
    public List<DismantlingLocationHitRecord> findByContainerNo(String containerNo) {
        Criteria criteria = Criteria.forClass(DismantlingLocationHitRecord.class);
        criteria.restrictionBuilder().eq(DismantlingLocationHitRecord::getContainerNo, containerNo).build();
        List<DismantlingLocationHitRecord> list = dismantlingLocationHitRecordMapper.findByCriteria(criteria);
        return list;
    }
}
