package fcb.project.manager.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import fcb.project.manager.base.entity.ContextTokenBean;
import fcb.project.manager.base.entity.TmAudit;
import fcb.project.manager.base.dao.TmAuditDao;
import fcb.project.manager.base.entity.TmEstate;
import fcb.project.manager.base.entity.TmHouse;
import fcb.project.manager.base.enums.AuditStatus;
import fcb.project.manager.base.enums.DeleteStatus;
import fcb.project.manager.base.enums.SysUserRoleEnums;
import fcb.project.manager.base.service.ITmAuditService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fdage.back.sdk.base.uuid.SnowFlakeUUidUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 审核表 服务实现类
 * </p>
 *
 * @author abner
 * @since 2021-01-08
 */
@Service
@Log4j2
public class TmAuditServiceImpl extends ServiceImpl<TmAuditDao, TmAudit> implements ITmAuditService {


    public IPage<TmAudit> queryOrSearchList(String vrTitle , Integer vrType,
                                            String belongEstate , String belongCompany,
                                            Integer auditStatus , long pageNum , long pageSize , String houseId){
        IPage<TmAudit> page = new Page<>(pageNum , pageSize);
        LambdaQueryWrapper<TmAudit> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNoneBlank(vrTitle)){
            lambdaQueryWrapper.like(TmAudit::getVrTitle , vrTitle);
        }
        if(StringUtils.isNotBlank(belongCompany)){
            lambdaQueryWrapper.eq(TmAudit::getBelongCompanyId , belongCompany);
        }
        if(StringUtils.isNotBlank(belongEstate)){
            lambdaQueryWrapper.like(TmAudit::getBelongEstateName , belongEstate);
        }
        if(null != auditStatus){
            lambdaQueryWrapper.eq(TmAudit::getAuditStatus , auditStatus);
        }else{
            lambdaQueryWrapper.ne(TmAudit::getAuditStatus , AuditStatus.WAITING_AUDIT.getCode());
            lambdaQueryWrapper.ne(TmAudit::getAuditStatus , AuditStatus.SHOOT_AUDIT.getCode());
        }
        if(StringUtils.isNotBlank(houseId)){
            lambdaQueryWrapper.eq(TmAudit::getVrId , houseId);
        }
        lambdaQueryWrapper.orderByDesc(TmAudit::getAuditTime);
        return getBaseMapper().selectPage(page , lambdaQueryWrapper);
    }

    public long countAuditNum(String vrId){
        LambdaQueryWrapper<TmAudit> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TmAudit::getVrId , vrId);
        lambdaQueryWrapper.eq(TmAudit::getIsDelete , 0);
        IPage<TmAudit> page = new Page<>(1 , 10);
        IPage<TmAudit> resultPage = getBaseMapper().selectPage(page , lambdaQueryWrapper);
        return null != resultPage ? resultPage.getTotal() : 0L;
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateAudit(TmAudit tmAudit){
        if(null == tmAudit){
            return -1;
        }
        if(null == tmAudit.getId()){
            return -1;
        }
        tmAudit.setUpdateTime(LocalDateTime.now());
        return getBaseMapper().updateById(tmAudit);
    }

    public TmAudit getByHouseId(String houseId){
        if(StringUtils.isBlank(houseId)){
            return null;
        }
        LambdaQueryWrapper<TmAudit> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TmAudit::getVrId , houseId);
        lambdaQueryWrapper.eq(TmAudit::getIsDelete , DeleteStatus.NOT_DELETE.getCode());
        lambdaQueryWrapper.last("limit 1");
        return getBaseMapper().selectOne(lambdaQueryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    public int deleteAudit(String id){
        if(StringUtils.isBlank(id)){
            return -1;
        }

        return getBaseMapper().deleteById(id);
    }

    public TmAudit selectForUpdate(String id){
        LambdaQueryWrapper<TmAudit> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TmAudit::getId , id);
        lambdaQueryWrapper.last("for update");
        return getBaseMapper().selectOne(lambdaQueryWrapper);
    }


    @Transactional(rollbackFor = Exception.class)
    public  TmAudit addNewAuditWithRespon(TmHouse tmHouse , TmEstate tmEstate , ContextTokenBean token){
        if(null == tmHouse){
            return null;
        }
        TmAudit tmAudit = new TmAudit();
        tmAudit.setId(SnowFlakeUUidUtils.generaUUid(null , null , "AUD00000"));
        tmAudit.setVrTitle(tmHouse.getHouseTitle());
        tmAudit.setVrId(tmHouse.getId());
        tmAudit.setVrType(tmHouse.getType());
        if(null != tmEstate){
            tmAudit.setBelongEstateName(tmEstate.getEstateName());
            tmAudit.setBelongEstateId(tmEstate.getId());
        }
        if(null != token){
            tmAudit.setCreatorId(token.getId());
            tmAudit.setCreatorName(token.getUserName());
        }
        tmAudit.setBelongCompanyId(tmEstate.getBelongCompanyId());
        tmAudit.setBelongCompanyName(tmEstate.getBelongCompanyName());
        tmAudit.setSumitTime(LocalDateTime.now());
        //默认审核状态为0，地区管理员提交的审核，若拍摄人员提交的，状态修改为2
        tmAudit.setAuditStatus(AuditStatus.WAITING_AUDIT.getCode());
        if(SysUserRoleEnums.REGION_SHOOT.getRoleKey().equals(token.getRoleKey())){
            tmAudit.setAuditStatus(AuditStatus.SHOOT_AUDIT.getCode());
        }
        tmAudit.setVrOnline(tmHouse.getOnline());
        tmAudit.setCreateTime(LocalDateTime.now());
        tmAudit.setUpdateTime(LocalDateTime.now());
        tmAudit.setIsDelete(DeleteStatus.NOT_DELETE.getCode());
        int insert =  getBaseMapper().insert(tmAudit);
        if(insert != 1){
            return null;
        }
        return tmAudit;
    }




}
