package com.mvc.service.produce;

import com.mvc.dao.TProductionPlanDetailMapper;
import com.mvc.entity.TProductionPlanDetail;
import com.mvc.entity.TProductionPlanDetailExample;
import com.mvc.model.produce.TProductionPlanDetailModel;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class TProductionPlanDetailService {

    @Resource
    private TProductionPlanDetailMapper productionPlanDetailMapper;

    public List<TProductionPlanDetailModel> findAllTProductionPlanDetail() {
        List<TProductionPlanDetail> TProductionPlanDetails = productionPlanDetailMapper.selectByExample(null);
        List<TProductionPlanDetailModel> resList = new ArrayList<>();
        for (TProductionPlanDetail TProductionPlanDetail : TProductionPlanDetails) {
            TProductionPlanDetailModel model = new TProductionPlanDetailModel();
            BeanUtils.copyProperties(TProductionPlanDetail, model);
            resList.add(model);
        }
        return resList;
    }

    /**
     * 根据条件查询列表
     * @param example
     * @return
     */
    private List<TProductionPlanDetailModel> selectByExample(TProductionPlanDetailExample example) {
        List<TProductionPlanDetail> deviceSnManagements = productionPlanDetailMapper.selectByExample(example);
        List<TProductionPlanDetailModel> resList = new ArrayList<>();
        for(TProductionPlanDetail deviceSnManagement : deviceSnManagements){
            TProductionPlanDetailModel model = new TProductionPlanDetailModel();
            BeanUtils.copyProperties(deviceSnManagement, model);
            resList.add(model);
        }
        return resList;
    }

    /**
     * 根据planId获取生产计划明细列表
     * @param planId
     * @return
     */
    public List<TProductionPlanDetailModel> findTProductionPlanDetail(int planId) {
        TProductionPlanDetailExample example = new TProductionPlanDetailExample();
        TProductionPlanDetailExample.Criteria criteria = example.createCriteria();
        criteria.andProductionPlanIdEqualTo(planId);
        return selectByExample(example);
    }

    public TProductionPlanDetailModel saveTProductionPlanDetail(TProductionPlanDetailModel model) {
        TProductionPlanDetail TProductionPlanDetail = new TProductionPlanDetail();
        BeanUtils.copyProperties(model, TProductionPlanDetail);
        int insert = productionPlanDetailMapper.insert(TProductionPlanDetail);
        model.setId(TProductionPlanDetail.getId());
        return model;
    }

    public TProductionPlanDetailModel updateTProductionPlanDetail(TProductionPlanDetailModel model) {
        TProductionPlanDetail TProductionPlanDetail = productionPlanDetailMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, TProductionPlanDetail);
        int i = productionPlanDetailMapper.updateByPrimaryKey(TProductionPlanDetail);
        return model;
    }

    public int deleteTProductionPlanDetail(int id) {
        int i = productionPlanDetailMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 根据planId删除生产计划明细
     * @param planId
     * @return
     */
    public int deleteTProductionPlanDetailByPlanId(int planId) {
        TProductionPlanDetailExample example = new TProductionPlanDetailExample();
        TProductionPlanDetailExample.Criteria criteria = example.createCriteria();
        criteria.andProductionPlanIdEqualTo(planId);

        int i = productionPlanDetailMapper.deleteByExample(example);
        return i;
    }
}
