package com.huatek.busi.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.common.SiteConstants.Status;
import com.huatek.busi.common.SiteConstants.TxTypeEnum;
import com.huatek.busi.dao.IecBaseConfDao;
import com.huatek.busi.dto.IecBaseConfDto;
import com.huatek.busi.entity.IecBaseConf;
import com.huatek.busi.mapper.IecBaseConfMapper;
import com.huatek.busi.service.BusiSiteService;
import com.huatek.busi.service.IecBaseConfService;
import com.huatek.busi.vo.BusiSiteVo;
import com.huatek.busi.vo.IecBaseConfVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.util.date.TimeUtil;
import com.huatek.torch.util.uuid.Uuid;

/**
 * IEC104-基础地址配置表 ServiceImpl
 *
 * @author anker_cheng
 * @date 2019-8-1 9:41:08
 */
@Component
@Service(interfaceClass = IecBaseConfService.class)
@Transactional(rollbackFor = Exception.class)
public class IecBaseConfServiceImpl implements IecBaseConfService {

    @Autowired
    private IecBaseConfMapper iecBaseConfMapper;
    
    @Autowired
    private IecBaseConfDao iecBaseConfDao;
    
    @Autowired
    private BusiSiteService busiSiteService;
    
    @Override
    public PageInfo<IecBaseConfVo> findIecBaseConfPage(IecBaseConfDto iecBaseConfDto) {
        return PageHelper.startPage(iecBaseConfDto.getPage(), iecBaseConfDto.getLimit())
                .doSelectPageInfo(() -> iecBaseConfMapper.selectIecBaseConfPage(iecBaseConfDto));
    }

    @Override
    public int addIecBaseConf(IecBaseConfDto iecBaseConfDto){
        IecBaseConf iecBaseConf = new IecBaseConf();
        iecBaseConfDto.setDTime(TimeUtil.getLocalDateTime());
        BeanUtils.copyPropertiesWithIgnoreNull(iecBaseConfDto, iecBaseConf);

        iecBaseConf.setStatus(Status.NORMAL.getValue());
        BusiSiteVo siteInfo = busiSiteService.getSiteInfo();
        if (!ObjectUtils.isEmpty(siteInfo)) {
        	iecBaseConf.setSiteId(siteInfo.getId());
        }
        iecBaseConf.setStartAddr(iecBaseConfDto.getYxStartAddr());
        iecBaseConf.setEndAddr(iecBaseConfDto.getYxEndAddr());
        iecBaseConf.setTxType(TxTypeEnum.YX.getValue());
        iecBaseConf.setId(Uuid.getUUID());
        int count = iecBaseConfDao.add(iecBaseConf);
        iecBaseConf.setStartAddr(iecBaseConfDto.getYcStartAddr());
        iecBaseConf.setEndAddr(iecBaseConfDto.getYcEndAddr());
        iecBaseConf.setTxType(TxTypeEnum.YC.getValue());
        iecBaseConf.setId(Uuid.getUUID());
        int ycresult = iecBaseConfDao.add(iecBaseConf);
        if (count >0 && ycresult >0) {
        	return 1;
        } else {
        	return 0;
        }
    }

    @Override
    public IecBaseConf findIecBaseConf(String id){
        return iecBaseConfDao.get(id);
    }

    @Override
    public int updateIecBaseConf(IecBaseConfDto iecBaseConfDto){
        IecBaseConf iecBaseConf = new IecBaseConf();
        BeanUtils.copyPropertiesWithIgnoreNull(iecBaseConfDto, iecBaseConf);
        if(StringUtils.isEmpty(iecBaseConf.getStatus())) {
            iecBaseConf.setStatus(Status.NORMAL.getValue());
        }
        int count = iecBaseConfDao.updateLocal(iecBaseConf);
        return count;
    }

    @Override
    public int deleteIecBaseConf(String[] ids){
    	int count = 0;
        if (ids == null || ids.length == 0) {
            return count;
        }
        for (String id : ids) {
        	IecBaseConf iecBaseConf = iecBaseConfDao.get(id);
            if(ObjectUtils.isEmpty(iecBaseConf)) {
                continue;
            }
            iecBaseConf.setStatus(Status.DELETE.getValue());
            count += iecBaseConfDao.updateLocal(iecBaseConf);
        }
        return count;
    }
}