package com.zd.bottle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.zd.bottle.domain.UsegasApply;
import com.zd.bottle.domain.UsegasAuditRecord;
import com.zd.bottle.mapper.UsegasAuditRecordMapper;
import com.zd.bottle.service.UsegasAuditRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zd.common.security.service.TokenService;
import jdk.nashorn.internal.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用气审核表 服务实现类
 * </p>
 *
 * @author hanson
 * @since 2022-09-08
 */
@Service
public class UsegasAuditRecordServiceImpl extends ServiceImpl<UsegasAuditRecordMapper, UsegasAuditRecord> implements UsegasAuditRecordService {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private UsegasApplyServiceImpl usegasApplyService;

    @Override
    public List <UsegasAuditRecord> auditRecordList(UsegasAuditRecord usegasAuditRecord) {
        LambdaQueryWrapper <UsegasAuditRecord> wrapper = Wrappers.lambdaQuery();
        wrapper.ge(UsegasAuditRecord::getUsegasApplyId,usegasAuditRecord.getUsegasApplyId());
        wrapper.setEntity(usegasAuditRecord);
        List <UsegasAuditRecord> list = list(wrapper);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int auditPass(UsegasAuditRecord usegasAuditRecord) {
        Boolean flag = usegasApplyService.updatetUseagsApply(usegasAuditRecord.getUsegasApplyId(),usegasAuditRecord.getAuditStatus());
        if(flag){
            usegasAuditRecord.setAuditUserid(tokenService.getLoginUser().getUserid());
            usegasAuditRecord.setAuditUser(tokenService.getLoginUser().getNickName());
            usegasAuditRecord.setAuditTime(new Date());
            usegasAuditRecord.setCreateBy(tokenService.getLoginUser().getNickName());
            usegasAuditRecord.setCreateTime(new Date());
            save(usegasAuditRecord);
        }
        return 1;
    }

}
