package com.huatek.busi.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.common.SiteConstants.Status;
import com.huatek.busi.dao.BusiSiteDao;
import com.huatek.busi.dao.SiteUploadCycleDao;
import com.huatek.busi.dto.BusiSiteDto;
import com.huatek.busi.dto.SiteUploadCycleDto;
import com.huatek.busi.entity.BusiSite;
import com.huatek.busi.entity.BusiSiteTree;
import com.huatek.busi.entity.SiteUploadCycle;
import com.huatek.busi.mapper.BusiSiteMapper;
import com.huatek.busi.mapper.SiteUploadCycleMapper;
import com.huatek.busi.service.BusiSiteService;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.vo.BusiSiteVo;
import com.huatek.busi.vo.SiteUploadCycleVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.util.tools.UserUtil;
import com.huatek.torch.util.uuid.Uuid;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 站点表 ServiceImpl
 *
 * @author anker_cheng
 * @date Jun 18, 2019 6:11:26 PM
 */
@Component
@Service(interfaceClass = BusiSiteService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiSiteServiceImpl implements BusiSiteService {

    @Autowired
    private BusiSiteMapper busiSiteMapper;
    
    @Autowired
    private SiteUploadCycleMapper siteUploadCycleMapper;
    
    @Autowired
    private SiteUploadCycleDao siteUploadCycleDao;
    
    @Autowired
    private BusiSiteDao busiSiteDao;
    
    @Reference(check = false)
    private BusiSiteTreeService busiSiteTreeService;

    @Override
    public PageInfo<BusiSiteVo> findBusiSitePage(BusiSiteDto busiSiteDto){
        return PageHelper.startPage(busiSiteDto.getPage(), busiSiteDto.getLimit())
            .doSelectPageInfo(() -> busiSiteMapper.selectBusiSitePage(busiSiteDto));
    }

    @Override
    public int addBusiSite(String token, BusiSiteDto busiSiteDto) {
        BusiSite busiSite = new BusiSite();
        BeanUtils.copyPropertiesWithIgnoreNull(busiSiteDto, busiSite);
        String siteId = Uuid.getUUID();
        busiSite.setId(siteId);
        busiSite.setCreateTime(LocalDateTime.now());

        LocalUser user = UserUtil.getLocalUser(token);
        if(user != null) {
            busiSite.setCreateName(user.getUserName());
            busiSite.setCreateUser(user.getUid());
        }
        if(StringUtils.isEmpty(busiSite.getStatus())) {
            busiSite.setStatus(Status.NORMAL.getValue());
        }

        int count = busiSiteDao.add(busiSite);
        // 新增站点树节点
        busiSiteTreeService.insertSiteNode(busiSite.getId(), busiSite.getSiteName());
        //新增站点配置
        SiteUploadCycle siteUploadCycle = new SiteUploadCycle();
        siteUploadCycle.setId(Uuid.getUUID());
        siteUploadCycle.setSiteId(siteId);
		if(!ObjectUtils.isEmpty(user)) {
			siteUploadCycle.setCreateName(user.getUserName());
			siteUploadCycle.setCreateUser(user.getUid());
		}
		siteUploadCycle.setCreateTime(LocalDateTime.now());
		siteUploadCycleDao.add(siteUploadCycle);
        return count;
    }

    @Override
    public BusiSite findBusiSite(String id){
        return busiSiteDao.get(id);
    }

    @Override
    public int updateBusiSite(String token, BusiSiteDto busiSiteDto){
        BusiSite busiSite = new BusiSite();
        BeanUtils.copyPropertiesWithIgnoreNull(busiSiteDto, busiSite);
        int count = busiSiteDao.updateLocal(busiSite);
        //更新站点树表信息
        busiSiteTreeService.updateSiteTreeName(busiSite.getId(), busiSite.getSiteName());
        return count;
    }

    @Override
    public boolean deleteBusiSite(String token, BusiSiteDto busiSiteDto){
        BusiSite site = new BusiSite();
        site.setId(busiSiteDto.getId());
        site.setStatus(SiteConstants.Status.DELETE.getValue());
        busiSiteDao.updateLocal(site);
        List<BusiSiteTree> list = busiSiteTreeService.findListBySiteId(busiSiteDto.getId());
        if (!list.isEmpty()) {
            list.forEach(tree -> {
                tree.setStatus(SiteConstants.Status.DELETE.getValue());
                busiSiteTreeService.deleteNode(tree.getId());
            });
        }
        return true;
    }

    /**
     **  获取站点监测装置上传数据时间信息
     */
	@Override
	public SiteUploadCycleVo getSiteUploadCycleInfo(String siteId) {
		return siteUploadCycleMapper.selectSiteUploadCycleInfo(siteId);
	}

	/**
	 * 维护站点监测装置上传数据时间设置
	 */
	@Override
	public int saveSiteUploadCycleInfo(String token, SiteUploadCycleDto siteUploadCycleDTO) {
		SiteUploadCycle siteUploadCycle = new SiteUploadCycle();
	    BeanUtils.copyPropertiesWithIgnoreNull(siteUploadCycleDTO, siteUploadCycle);
	    int result = 0;
		if (!ObjectUtils.isEmpty(siteUploadCycle.getId())) {
			result = siteUploadCycleDao.updateLocal(siteUploadCycle);
		} else {
			siteUploadCycle.setId(Uuid.getUUID());
			LocalUser user = UserUtil.getLocalUser(token);
			if(!ObjectUtils.isEmpty(user)) {
				siteUploadCycle.setCreateName(user.getUserName());
				siteUploadCycle.setCreateUser(user.getUid());
			}
			siteUploadCycle.setCreateTime(LocalDateTime.now());
			result = siteUploadCycleDao.add(siteUploadCycle);
		}
		return result;
	}

	  /**
     *  获取所有站点信息（视频流需要引用）
     * @return
     */
    @Override
    public List<BusiSiteVo> queryAllSite() {
        return busiSiteMapper.queryAllSite();
    }
    
    /**
     * 获取站点信息
     * @return
     */
    @Override
    public BusiSiteVo getSiteInfo() {
        return busiSiteMapper.getSiteInfo();
    }

    @Override
    public List<BusiSiteDto> findAll(){
        return busiSiteMapper.findAll();
    }

    @Override
    public BusiSiteDto findBySiteName(String siteName){
        return busiSiteMapper.findBySiteName(siteName);
    }

    @Override
    public List<BusiSiteVo> findSiteInfo() {
        return busiSiteMapper.findSiteInfo();
    }
}