package com.mvc.service.upgrade;

import com.mvc.constant.DeviceUpgradeStatus;
import com.mvc.dao.TUpgradePlanDetailMapper;
import com.mvc.entity.TUpgradePlanDetail;
import com.mvc.entity.TUpgradePlanDetailExample;
import com.mvc.model.upgrade.TUpgradePlanDetailModel;
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 TUpgradePlanDetailService {

    @Resource
    private TUpgradePlanDetailMapper tUpgradePlanDetailMapper;

    /**
     * 查询全部记录
     * @return
     */
    public List<TUpgradePlanDetailModel> findAllTUpgradePlanDetail() {
        List<TUpgradePlanDetail> tUpgradePlanDetails = tUpgradePlanDetailMapper.selectByExample(null);
        return getTUpgradePlanDetailModelList(tUpgradePlanDetails);
    }

    /**
     * 保存新增对象
     * @param model
     * @return
     */
    public TUpgradePlanDetailModel saveTUpgradePlanDetail(TUpgradePlanDetailModel model) {
        model.setUpgradeStatus(DeviceUpgradeStatus.UNEXECUTED.getFlag());
        model.setFailNum(0);
        TUpgradePlanDetail tUpgradePlanDetail = new TUpgradePlanDetail();
        BeanUtils.copyProperties(model,tUpgradePlanDetail);
        tUpgradePlanDetailMapper.insert(tUpgradePlanDetail);
        model.setId(tUpgradePlanDetail.getId());
        return model;
    }

    /**
     * 修改对象
     * @param model
     * @return
     */
    public TUpgradePlanDetailModel updateTUpgradePlanDetail(TUpgradePlanDetailModel model) {
        TUpgradePlanDetail tUpgradePlanDetail = tUpgradePlanDetailMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model,tUpgradePlanDetail);
        int i = tUpgradePlanDetailMapper.updateByPrimaryKey(tUpgradePlanDetail);
        return model;
    }

    /**
     * 删除对象
     * @param id
     * @return
     */
    public int deleteTUpgradePlanDetail(int id) {
        int i = tUpgradePlanDetailMapper.deleteByPrimaryKey(id);
        return i;
    }

    public List<TUpgradePlanDetailModel> findTUpgradePlanDetailsByPlanId(int planId) {
        TUpgradePlanDetailExample example=new TUpgradePlanDetailExample();
        TUpgradePlanDetailExample.Criteria criteria=example.createCriteria();
        criteria.andPlanIdEqualTo(planId);
        List<TUpgradePlanDetail> details = tUpgradePlanDetailMapper.selectByExample(example);
        return getTUpgradePlanDetailModelList(details);
    }

    public List<TUpgradePlanDetailModel> getTUpgradePlanDetailModelList(List<TUpgradePlanDetail> details){
        List<TUpgradePlanDetailModel> resList=new ArrayList<>();
        for(TUpgradePlanDetail TUpgradePlanDetail:details){
            TUpgradePlanDetailModel model = new TUpgradePlanDetailModel();
            BeanUtils.copyProperties(TUpgradePlanDetail,model);
            resList.add(model);
        }
        return resList;
    }

    /**
     * 根据PlanId删除所有明细
     * @param planId
     * @return
     */
    public int deleteTUpgradePlanDetailsByPlanId(int planId) {
        TUpgradePlanDetailExample example=new TUpgradePlanDetailExample();
        TUpgradePlanDetailExample.Criteria criteria=example.createCriteria();
        criteria.andPlanIdEqualTo(planId);
        int i = tUpgradePlanDetailMapper.deleteByExample(example);
        return i;
    }

    /**
     * 根据deviceSn获取PlanId列表(前台根据站码查询时使用)
     * @param deviceSn
     * @return
     */
    public List<Integer> findPlanIdListByDeviceSn(String deviceSn) {
        TUpgradePlanDetailExample example=new TUpgradePlanDetailExample();
        TUpgradePlanDetailExample.Criteria criteria=example.createCriteria();
        criteria.andDeviceSnEqualTo(deviceSn);

        List<TUpgradePlanDetail> details = tUpgradePlanDetailMapper.selectByExample(example);
        List<Integer> list=new ArrayList<>();
        for(TUpgradePlanDetail detail: details){
            list.add(detail.getPlanId());
        }
        return list;
    }
}
