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

import java.time.LocalDateTime;
import java.util.Arrays;
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.BusiOfflineTransformerCheckDao;
import com.huatek.busi.dto.BusiOfflineTransformerCheckDto;
import com.huatek.busi.entity.BusiOfflineTransformerCheck;
import com.huatek.busi.mapper.BusiOfflineTransformerCheckMapper;
import com.huatek.busi.service.transformer.BusiOfflineTransformerCheckService;
import com.huatek.busi.vo.BusiOfflineTransformerCheckVo;
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;

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

    @Autowired
    private BusiOfflineTransformerCheckMapper busiOfflineTransformerCheckMapper;
    @Autowired
    private BusiOfflineTransformerCheckDao busiOfflineTransformerCheckDao;

    @Override
    public PageInfo<BusiOfflineTransformerCheckVo> findBusiOfflineTransformerCheckPage(BusiOfflineTransformerCheckDto busiOfflineTransformerCheckDto){
        return PageHelper.startPage(busiOfflineTransformerCheckDto.getPage(), busiOfflineTransformerCheckDto.getLimit())
            .doSelectPageInfo(() -> busiOfflineTransformerCheckMapper.selectBusiOfflineTransformerCheckPage(busiOfflineTransformerCheckDto));
    }

    @Override
    public BusiOfflineTransformerCheckVo findBusiOfflineTransformerCheckVo(String id){
        return busiOfflineTransformerCheckMapper.getBusiOfflineTransformerCheckVo(id);
    }

    @Override
    public void deleteBusiOfflineTransformerCheck(String[] ids){
    	List<String> asList = Arrays.asList(ids);
    	busiOfflineTransformerCheckMapper.batchDel(asList);
    }

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