package com.yeejoin.amos.boot.module.jxiop.biz.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeejoin.amos.boot.module.jxiop.api.dto.StationCoordinateDto;
import com.yeejoin.amos.boot.module.jxiop.api.entity.StationBasic;
import com.yeejoin.amos.boot.module.jxiop.api.entity.StationCoordinate;
import com.yeejoin.amos.boot.module.jxiop.api.mapper.StationBasicMapper;
import com.yeejoin.amos.boot.module.jxiop.api.service.IStationBasicService;
import com.yeejoin.amos.boot.module.jxiop.api.dto.StationBasicDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.typroject.tyboot.core.rdbms.service.BaseService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * 场站基础信息表服务实现类
 *
 * @author system_generator
 * @date 2023-04-20
 */
@Service
public class StationBasicServiceImpl extends BaseService<StationBasicDto,StationBasic,StationBasicMapper> implements IStationBasicService {


    @Autowired
    StationCoordinateServiceImpl stationCoordinateServiceImpl;
    @Autowired
    StationBasicMapper stationBasicMapper;
    /**
     * 分页查询
     */
    public Page<StationBasicDto> queryForStationBasicPage(Page<StationBasicDto> page)   {
        return this.queryForPage(page, null, false);
    }


    /**
     * 分页查询
     */
    public Page<StationBasicDto> queryPage(Page<StationBasicDto> page,
                                           String stationMasterName,
                                           String stationName,
                                           String stationType)   {
        List<StationBasicDto> list=stationBasicMapper.queryPage((page.getCurrent()-1) * page.getSize(),page.getSize(),  stationMasterName,
                stationName,
                stationType);
        List<StationBasicDto> listcount=stationBasicMapper.queryPagecount( stationMasterName,
                stationName,
                stationType);
        page.setTotal(listcount.size());
        page.setRecords(list);
        return page;
    }





    /**
     * 列表查询 示例
     */
    public List<StationBasicDto> queryForStationBasicList()   {
        return this.queryForList("" , false);
    }

    @Transactional
    public void add(StationBasic  stationBasic)   {
        //场站坐标信息
        List<StationCoordinate>  list=  stationBasic.getStationCoordinate();
        List<StationCoordinate>  listdate=new ArrayList<>();
        if(stationBasic.getBelongArealist()!=null){
            stationBasic.setBelongArea(JSON.toJSONString(stationBasic.getBelongArealist()));
        }

        //场站基本信息
        this.save(stationBasic);

        for (StationCoordinate stationCoordinate : list) {
            stationCoordinate.setStationId(stationBasic.getSequenceNbr());
            listdate.add(stationCoordinate);
        }

        stationCoordinateServiceImpl.saveBatch(listdate);


    }



    public void update(StationBasic  stationBasic)   {
        //场站坐标信息
        List<StationCoordinate>  list=  stationBasic.getStationCoordinate();
        List<StationCoordinate>  listdate=new ArrayList<>();
        if(stationBasic.getBelongArealist()!=null){
            stationBasic.setBelongArea(JSON.toJSONString(stationBasic.getBelongArealist()));
        }
        //场站基本信息
        stationBasicMapper.updateById(stationBasic);


        for (StationCoordinate stationCoordinate : list) {
            stationCoordinate.setStationId(stationBasic.getSequenceNbr());
            listdate.add(stationCoordinate);
        }
        stationCoordinateServiceImpl.saveOrUpdateBatch(listdate);

    }


    public   int deleteList(String[] ids){

           return stationBasicMapper.deleteList(ids);
         }


    public  StationBasic getById(Long sequenceNbr){

        QueryWrapper<StationBasic> wrapper1 = new QueryWrapper();
        wrapper1.eq("sequence_nbr",sequenceNbr);
        StationBasic  stationBasic=this.getOne(wrapper1);

        if(stationBasic.getBelongArea()!=null){
            stationBasic.setBelongArealist(JSON.parseArray(stationBasic.getBelongArea(),Integer.class));
        }

        QueryWrapper<StationCoordinate> wrapper = new QueryWrapper();
        wrapper.eq("station_id",sequenceNbr);
        List<StationCoordinate>  list=stationCoordinateServiceImpl.list(wrapper);
        stationBasic.setStationCoordinate(list);
        return stationBasic;

    }


}