package com.mvc.service.app;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.AppDevgroupDetailMapper;
import com.mvc.entity.AppDevgroupDetail;
import com.mvc.entity.AppDevgroupDetailExample;
import com.mvc.entity.StationAcc;
import com.mvc.entity.TaskBatchRemoteDetail;
import com.mvc.model.AppDevgroupDetailModel;
import com.mvc.model.StationAccModel;
import com.mvc.model.TAireadModel;
import com.mvc.model.TaskBatchRemoteDetailModel;
import com.mvc.service.copy.TAireadService;
import com.mvc.service.taizhang.StationAccService;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class AppDevgroupDetailService {

    @Resource
    private AppDevgroupDetailMapper appDevgroupDetailMapper;

    @Resource
    private TAireadService aidService;

    @Resource
    private StationAccService stationAccService;

    public List<AppDevgroupDetailModel> findAllAppDevgroupDetailByGroupid(String groupid) {
        AppDevgroupDetailExample example = new AppDevgroupDetailExample();
        AppDevgroupDetailExample.Criteria criteria = example.createCriteria();
        criteria.andGroupIdEqualTo(groupid);
        List<AppDevgroupDetail> AppDevgroupDetails = appDevgroupDetailMapper.selectByExample(example);
        List<AppDevgroupDetailModel> resList = new ArrayList<>();
        for (AppDevgroupDetail detail : AppDevgroupDetails) {
            AppDevgroupDetailModel model = new AppDevgroupDetailModel();
            BeanUtils.copyProperties(detail, model);
            resList.add(model);
        }
        return resList;
    }

    /**
     * 根据条件获取站点台账列表
     * @param queryModel
     * @return
     */
    public Map<String, Object> findAllStationAccsByCondition(AppDevgroupDetailModel queryModel) {
        AppDevgroupDetailExample example = new AppDevgroupDetailExample();
        AppDevgroupDetailExample.Criteria criteria = example.createCriteria();
        criteria.andGroupIdEqualTo(queryModel.getGroupId());

        Map<String, Object> resMap = new HashMap<>();
        Page<Object> pageObject = PageHelper.startPage(queryModel.getPage(), queryModel.getRows());
        List<AppDevgroupDetail> TaskBatchRemoteDetails = appDevgroupDetailMapper.selectByExample(example);

        List<StationAccModel> resList = new ArrayList<>();
        for (AppDevgroupDetail detail : TaskBatchRemoteDetails) {
            StationAccModel accModel = stationAccService.findStationAccByStationCode(detail.getStationCode());
            if (accModel != null) {
                TAireadModel aireadModel = aidService.findLatesTAireadByStationCode(detail.getStationCode());
                if (aireadModel != null) {
                    accModel.settAireadModel(aireadModel);
                }
                resList.add(accModel);
            }
        }
        resMap.put("total", pageObject.getTotal());
        resMap.put("list", resList);
        return resMap;
    }

    public AppDevgroupDetailModel saveAppDevgroupDetail(AppDevgroupDetailModel model) {
        model.setDetailId(StringUtil.getUuid());
        model.setCreateTime(new Date());
        AppDevgroupDetail AppDevgroupDetail = new AppDevgroupDetail();
        BeanUtils.copyProperties(model, AppDevgroupDetail);
        int insert = appDevgroupDetailMapper.insert(AppDevgroupDetail);
        model.setId(AppDevgroupDetail.getId());
        return model;
    }

    public AppDevgroupDetailModel updateAppDevgroupDetail(AppDevgroupDetailModel model) {
        AppDevgroupDetail AppDevgroupDetail = appDevgroupDetailMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, AppDevgroupDetail);
        int i = appDevgroupDetailMapper.updateByPrimaryKey(AppDevgroupDetail);
        return model;
    }

    public int deleteAppDevgroupDetail(int id) {
        int i = appDevgroupDetailMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 根据组ID,删除明细所有记录
     *
     * @param groupId
     * @param stationCode
     * @return
     */
    public int deleteAppDevgroupDetailByGroupId(String groupId,String stationCode) {
        AppDevgroupDetailExample example = new AppDevgroupDetailExample();
        AppDevgroupDetailExample.Criteria criteria = example.createCriteria();
        criteria.andGroupIdEqualTo(groupId);
        if(StringUtils.isNotEmpty(stationCode)){
            criteria.andStationCodeEqualTo(stationCode);
        }
        return appDevgroupDetailMapper.deleteByExample(example);
    }

    /**
     * 检查数据是否合法
     * @param model
     * @return
     */
    public String checkDataLegitimacy(AppDevgroupDetailModel model){
        List<AppDevgroupDetailModel> groups = findAllAppDevgroupDetailByGroupid(model.getGroupId());
        for(AppDevgroupDetailModel group:groups){
            if(group.getStationCode().equals(model.getStationCode())){
                return String.format("站点[%s]已存在", model.getStationCode());
            }
        }
        return null;
    }

    /**
     * 根据组ID,统计明细记录条数
     * @param groupId
     * @return
     */
    public int countByGroupId(String groupId){
        AppDevgroupDetailExample example = new AppDevgroupDetailExample();
        AppDevgroupDetailExample.Criteria criteria = example.createCriteria();
        criteria.andGroupIdEqualTo(groupId);
        return (int) appDevgroupDetailMapper.countByExample(example);
    }
}
