package com.huatek.busi.service.impl.gis;

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

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 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.dao.BusiOfflineGisCheckDao;
import com.huatek.busi.dto.BusiOfflineGisCheckDto;
import com.huatek.busi.entity.BusiOfflineGisCheck;
import com.huatek.busi.mapper.BusiOfflineGisCheckMapper;
import com.huatek.busi.service.gis.BusiOfflineGisCheckService;
import com.huatek.busi.vo.BusiOfflineGisCheckVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.frame.exception.OwlException;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.util.tools.UserUtil;

/**
 * GIS投运验收结论 ServiceImpl
 *
 * @author anker_cheng
 * @date Jul 4, 2019 2:15:39 PM
 */
@Component
@Service(interfaceClass = BusiOfflineGisCheckService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiOfflineGisCheckServiceImpl implements BusiOfflineGisCheckService {

    @Autowired
    private BusiOfflineGisCheckMapper busiOfflineGisCheckMapper;
    @Autowired
    private BusiOfflineGisCheckDao busiOfflineGisCheckDao;

    @Override
    public PageInfo<BusiOfflineGisCheckVo> findBusiOfflineGisCheckPage(BusiOfflineGisCheckDto busiOfflineGisCheckDto){
        return PageHelper.startPage(busiOfflineGisCheckDto.getPage(), busiOfflineGisCheckDto.getLimit())
            .doSelectPageInfo(() -> busiOfflineGisCheckMapper.selectBusiOfflineGisCheckPage(busiOfflineGisCheckDto));
    }

    @Override
    public BusiOfflineGisCheckVo findBusiOfflineGisCheckVo(String id){
        return busiOfflineGisCheckMapper.getBusiOfflineGisCheckVo(id);
    }

    @Override
    public int deleteBusiOfflineGisCheck(String token, List<String> ids){
        return busiOfflineGisCheckMapper.batchDel(ids);
    }

    @Override
    public synchronized int saveOrUpdate(String token, BusiOfflineGisCheckDto busiOfflineGisCheckDto) {
     // 重复性校验
        String equipmentName = busiOfflineGisCheckMapper
            .checkExist(busiOfflineGisCheckDto.getEquipmentId(), busiOfflineGisCheckDto.getId());
        if(!StringUtils.isEmpty(equipmentName)) {
            throw new OwlException(equipmentName + "记录已存在，不能重复添加");
        }
        // 操作用户
        LocalUser user = UserUtil.getLocalUser(token);
        //保存或更新
        BusiOfflineGisCheck busiOfflineGisCheck = new BusiOfflineGisCheck();
        BeanUtils.copyPropertiesWithIgnoreNull(busiOfflineGisCheckDto, busiOfflineGisCheck);
        busiOfflineGisCheck.setStatus(Status.NORMAL.getValue());
        if(StringUtils.isEmpty(busiOfflineGisCheck.getId())) {
            busiOfflineGisCheck.setCreateTime(LocalDateTime.now());
            if(user != null) {
                busiOfflineGisCheck.setCreateUser(user.getUid());
                busiOfflineGisCheck.setCreateName(user.getUserName());
            }
        }
        return busiOfflineGisCheckDao.saveOrUpdate(busiOfflineGisCheck);
    }

}