/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.watermeter.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.watermeter.entity.Qfkgjl;
import com.tofly.watermeter.entity.Qfkhjbxx;
import com.tofly.watermeter.mapper.QfkgjlMapper;
import com.tofly.watermeter.service.QfkgjlService;
import com.tofly.watermeter.service.QfkhjbxxService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 开关记录表
 *
 * @author wangjieliu
 * @date Wed Aug 18 00:00:00 CST 2021
 */
@Service("qfkgjlService")
@AllArgsConstructor
public class QfkgjlServiceImpl extends ServiceImpl<QfkgjlMapper, Qfkgjl> implements QfkgjlService {

    private final QfkhjbxxService qfkhjbxxService;

    @Override
    public boolean saveSwitch(Qfkgjl qfkgjl) {
        String fmzt = qfkgjl.getFmzt();
        if (!"0".equals(fmzt) && !"1".equals(fmzt)) {
            throw new ToflyDeniedException("请正确选择阀门开关状态");
        }
        String yhbh = qfkgjl.getYhbh();
//        String cj = qfkgjl.getSbcj();
//        String lx = qfkgjl.getSblx();
//        String kj = qfkgjl.getSbkj();
//        if (StringUtil.isEmpty(cj)){
//            throw new ToflyDeniedException("请传入水表厂家");
//        }
//        if (StringUtil.isEmpty(lx)){
//            throw new ToflyDeniedException("请传入水表类型");
//        }
//        if (StringUtil.isEmpty(kj)){
//            throw new ToflyDeniedException("请传入水表口径");
//        }
        if (!StringUtils.isEmpty(yhbh)) {
            Qfkhjbxx qfkhjbxx = qfkhjbxxService.getOne(Wrappers.<Qfkhjbxx>lambdaQuery().eq(Qfkhjbxx::getYhbh, yhbh)
                    .select(Qfkhjbxx::getYhxm, Qfkhjbxx::getYhdz, Qfkhjbxx::getLxdh));

            if (qfkhjbxx != null) {
                qfkgjl.setYhxm(qfkhjbxx.getYhxm());
                qfkgjl.setYhdz(qfkhjbxx.getYhdz());
                qfkgjl.setLxdh(qfkhjbxx.getLxdh());
            }
        }
        qfkgjl.setKgTime(new Date());
        baseMapper.insert(qfkgjl);
        return true;
    }
}
