package bus.service.impl;

import bus.model.SnowflakeUtil;
import bus.model.dto.BStationInfoDto;
import bus.model.dto.BStationInfoPageDto;
import bus.model.po.BBusInfoPo;
import bus.model.vo.BStationInfoVo;
import bus.model.po.BStationInfoPo;
import bus.mapper.BStationInfoMapper;

import bus.service.BStationInfoService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orcas.common.sso.model.ExtInfo;
import com.orcas.iso.config.common.user.CurrentUserHolder;
import com.qzwisdom.qzframework.core.tool.exception.BusinessException;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Program: bus
 * @Description: 描述
 * @Author: zy
 * @Date: 2025-03-01 21:07:50
 **/
@Service
public class BStationInfoServiceImpl extends ServiceImpl<BStationInfoMapper, BStationInfoPo> implements BStationInfoService {

    @Autowired
    private BStationInfoMapper bStationInfoMapper;
    @Resource
    private SnowflakeUtil snowflakeUtil;

	/**
     * 详情
     * @param id
     * @return
     */
    @Override
    public BStationInfoDto getDetailById(String id){
        BStationInfoPo bStationInfoPo = this.bStationInfoMapper.selectById(id);
        BStationInfoDto bStationInfoDto = BeanUtil.toBean(bStationInfoPo, BStationInfoDto.class);
        if (bStationInfoPo.getPicUrl() != null) {
            bStationInfoDto.setPicUrlList(Arrays.asList(bStationInfoPo.getPicUrl().split(",")));
        }
        return bStationInfoDto;
    }

	/**
     * 保存
     * @param dto
     * @return
     */
    @Override
	public void save(BStationInfoDto dto){
        BStationInfoPo existingBus = this.bStationInfoMapper.selectOne(
                new QueryWrapper<BStationInfoPo>()
                        .eq("is_delete",0)
                        .eq("name", dto.getName())
        );
        if (existingBus != null) {
            throw new BusinessException("标志点名称已存在");
        }
        BStationInfoPo po = new BStationInfoPo();
        BeanUtils.copyProperties(dto,po);
        List<String> picUrlList = dto.getPicUrlList();
        if (picUrlList != null && picUrlList.size() > 0) {
            po.setPicUrl(StrUtil.join(",", picUrlList));
        }
        ExtInfo extInfo = CurrentUserHolder.get();
        po.setId(snowflakeUtil.snowflakeId());
        po.setCreateTime(new Date());
        po.setUpdateTime(new Date());
        po.setCreatorId(extInfo.getUserId());
        po.setUpdaterId(extInfo.getUserId());
        po.setCreatorName(extInfo.getUserName());
        po.setUpdaterName(extInfo.getUserName());
		this.bStationInfoMapper.insert(po);
    }

    /**
    * 列表
    * @param dto
    * @return
    */
    @Override
    public List<BStationInfoDto> list(BStationInfoPageDto dto){
        return this.bStationInfoMapper.list(dto);
    }

	/**
     * 修改
     * @param dto
     * @return
     */
    @Override
    public void update(BStationInfoDto dto){
        BStationInfoPo existingBus = this.bStationInfoMapper.selectOne(
                new QueryWrapper<BStationInfoPo>()
                        .eq("is_delete",0)
                        .eq("name", dto.getName())
                        .ne("id",dto.getId())
        );
        if (existingBus != null) {
            throw new BusinessException("标志点名称已存在");
        }
        BStationInfoPo po = new BStationInfoPo();
        BeanUtils.copyProperties(dto,po);
        List<String> picUrlList = dto.getPicUrlList();
        if (picUrlList != null && picUrlList.size() > 0) {
            po.setPicUrl(StrUtil.join(",", picUrlList));
        }
        ExtInfo extInfo = CurrentUserHolder.get();
        po.setUpdateTime(new Date());
        po.setUpdaterId(extInfo.getUserId());
        po.setUpdaterName(extInfo.getUserName());
		bStationInfoMapper.updateById(po);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public void delete(String id){
		bStationInfoMapper.deleteById(id);
    }
}
