package com.hfkj.service.order.impl;

import com.hfkj.dao.BsOrderChildMapper;
import com.hfkj.entity.BsOrderChild;
import com.hfkj.entity.BsOrderChildExample;
import com.hfkj.model.order.OrderChildModel;
import com.hfkj.service.order.BsOrderChildService;
import com.hfkj.sysenum.order.OrderChildStatusEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @className: BsOrderChildServiceImpl
 * @author: HuRui
 * @date: 2024/5/6
 **/
@Service("orderChildService")
public class BsOrderChildServiceImpl implements BsOrderChildService {
    @Resource
    private BsOrderChildMapper orderChildMapper;

    @Override
    public void editData(BsOrderChild data) {
        data.setUpdateTime(new Date());
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            orderChildMapper.insert(data);
        } else {
            orderChildMapper.updateByPrimaryKey(data);
        }
    }

    @Override
    public OrderChildModel getDetail(String childOrderNo) {
        BsOrderChildExample example = new BsOrderChildExample();
        example.createCriteria().andChildOrderNoEqualTo(childOrderNo);
        List<BsOrderChild> list = orderChildMapper.selectByExample(example);
        if (!list.isEmpty()) {
            OrderChildModel childModel = new OrderChildModel();
            BeanUtils.copyProperties(list.get(0), childModel);
            return childModel;
        }
        return null;

    }

    @Override
    public List<OrderChildModel> getOrderChildListByOrderNo(String orderNo) {
        BsOrderChildExample example = new BsOrderChildExample();
        example.createCriteria().andOrderNoEqualTo(orderNo);
        example.setOrderByClause("id");
        List<BsOrderChild> list = orderChildMapper.selectByExample(example);
        List<OrderChildModel> orderChildModelList = new ArrayList<>();
        for (BsOrderChild orderChild : list) {
            OrderChildModel childModel = new OrderChildModel();
            BeanUtils.copyProperties(orderChild, childModel);
            orderChildModelList.add(childModel);
        }
        return orderChildModelList;
    }

    @Override
    public long getUndoneChildOrder(String orderNo) {
        BsOrderChildExample example = new BsOrderChildExample();
        example.createCriteria().andOrderNoEqualTo(orderNo).andStatusNotEqualTo(OrderChildStatusEnum.status3.getCode());
        return orderChildMapper.countByExample(example);
    }
}
