package org.ta.pddserver.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.ta.pdd.entity.PickRecordEntity;
import org.ta.pddserver.mapper.PickRecordMapper;
import org.ta.pddserver.model.api.request.PackageNoticeRequest;
import org.ta.pddserver.service.PickRecordService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PickRecordImpl extends ServiceImpl<PickRecordMapper, PickRecordEntity> implements PickRecordService {


    public Map<String, Object> recordList(PackageNoticeRequest param) {

        Integer count = baseMapper.countUsable(param);
        param.checkPage(count,20);
        List<PickRecordEntity> list = baseMapper.getListByRequest(param);
        Map<String, Object> map = new HashMap<>();
        map.put("total", count);
        map.put("list", list);
        map.put("page", param.getPage());
        return map;
    }

    public PickRecordEntity checkAddRecord(String orderCode, int type) {
        PickRecordEntity pickRecordEntity = this.baseMapper.findByOrderCode(orderCode);
        if (pickRecordEntity == null) {
            pickRecordEntity = new  PickRecordEntity();
            pickRecordEntity.setOrderCode(orderCode);
//            pickRecordEntity.setCreator(SecurityContextHolder.getContext().getAuthentication().getName());
            pickRecordEntity.setCreated(new Date());
            pickRecordEntity.setType(type);
            this.save(pickRecordEntity);
        }
        return pickRecordEntity;
    }

    public boolean checkAddReceiver(String orderCode) {
        PickRecordEntity pickRecordEntity = this.baseMapper.findByOrderCode(orderCode);
        if (pickRecordEntity.getReceiver() == null) {
//            pickRecordEntity.setReceiver(SecurityContextHolder.getContext().getAuthentication().getName());
            pickRecordEntity.setReceiveTime(new Date());
            this.updateById(pickRecordEntity);
            return true;
        }
        return false;
    }

    public PickRecordEntity getByOrderCode(String orderCode) {
        return this.baseMapper.findByOrderCode(orderCode);
    }

    public boolean saveFinisher(PickRecordEntity pickRecordEntity) {
        if (pickRecordEntity.getFinisher() == null) {
//            pickRecordEntity.setFinisher(SecurityContextHolder.getContext().getAuthentication().getName());
            pickRecordEntity.setFinishTime(new Date());
            this.updateById(pickRecordEntity);
            return true;
        }
        return false;
    }
}