package com.aidex.system.service.impl;

import java.util.List;

import com.aidex.common.plush.domain.SysShipExtend;
import com.aidex.common.plush.service.SysShipExtendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.domain.BaseEntity;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.system.mapper.SysShipMapper;
import com.aidex.system.domain.SysShip;
import com.aidex.system.service.SysShipService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 船只管理Service业务层处理
 * @author ChenSir
 * @email 91476835
 * @date 2024-03-16
 */
@Service
@Transactional(readOnly = true)
public class SysShipServiceImpl extends BaseServiceImpl<SysShipMapper, SysShip> implements SysShipService {

    private static final Logger log = LoggerFactory.getLogger(SysShipServiceImpl.class);

    @Autowired
    private SysShipExtendService sysShipExtendService;

    /**
     * 获取单条数据
     * @param sysShip 船只管理
     * @return 船只管理
     */
    @Override
    public SysShip get(SysShip sysShip) {
        SysShip dto = super.get(sysShip);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 船只管理id
     * @return 船只管理
     */
    @Override
    public SysShip get(String id) {
        SysShip dto = super.get(id);
        return dto;
    }

    /**
     * 查询船只管理列表
     * @param sysShip 船只管理
     * @return 船只管理
     */
    @Override
    public List<SysShip> findList(SysShip sysShip) {
		List<SysShip> sysShipList = super.findList(sysShip);
        return sysShipList;
    }

    /**
     * 分页查询船只管理列表
     * @param sysShip 船只管理
     * @return 船只管理
     */
    @Override
    public PageInfo<SysShip> findPage(SysShip sysShip) {
		PageInfo<SysShip> page = super.findPage(sysShip);
        return page;
    }

    /**
     * 保存船只管理
     * @param sysShip
     * @return 结果
     */
    @Override
    public boolean save(SysShip sysShip) {
        if(super.save(sysShip)) {
            SysShipExtend shipExtend = new SysShipExtend();
            shipExtend.setShipId(sysShip.getId());
            shipExtend.setId(sysShip.getId());
            shipExtend.setMacId(sysShip.getShipNum());
            shipExtend.setDir("90");
            shipExtend.setIsStop(0L);
            shipExtend.setLat("36.077395");
            shipExtend.setLon("103.863987");
            shipExtend.setMileage("0");
            shipExtend.setDelFlag("0");
            if (sysShip.getIsNewRecord()) {
                sysShipExtendService.save(shipExtend);
            }else {
                if(sysShipExtendService.findById(sysShip.getId()) == null){
                    sysShipExtendService.save(shipExtend);
                }else {
                    sysShipExtendService.updateByShip(shipExtend);
                }
            }
            return Boolean.TRUE;
        }else
            return Boolean.FALSE;
    }

    /**
     * 删除船只管理信息
     * @param sysShip
     * @return 结果
     */
    @Override
    public boolean remove(SysShip sysShip) {
        if(super.remove(sysShip)) {
            sysShipExtendService.delByIds(sysShip.getId().split(","));
            return Boolean.TRUE;
        }else
            return Boolean.FALSE;
    }

    /**
     * 批量删除船只管理
     * @param ids 需要删除的船只管理ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteSysShipByIds(String[] ids) {
        sysShipExtendService.delByIds(ids);
        return mapper.deleteSysShipByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

    @Override
    public SysShip getShipByMacId(String macId) {
        return mapper.getShipByMacId(macId, BaseEntity.DEL_FLAG_NORMAL);
    }

    @Transactional(readOnly = false)
    @Override
    public int updateStatusByMacId(String macId, String status) {
        return mapper.updateStatusByMacId(macId,status);
    }

}
