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.TmOperation;
import fcb.project.manager.base.dao.TmOperationDao;
import fcb.project.manager.base.enums.UuidPreEnum;
import fcb.project.manager.base.service.ITmOperationService;
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.util.CollectionUtils;

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

/**
 * <p>
 * 操作记录表 服务实现类
 * </p>
 *
 * @author abner
 * @since 2021-01-13
 */
@Service
@Log4j2
public class TmOperationServiceImpl extends ServiceImpl<TmOperationDao, TmOperation> implements ITmOperationService {

    public int addNewWithTypeAndContent(ContextTokenBean contextTokenBean , String type , String content){
        TmOperation tmOperation = new TmOperation();
        tmOperation.setOperateTime(LocalDateTime.now());
        tmOperation.setOperatorId(contextTokenBean.getId());
        tmOperation.setOperatorName(contextTokenBean.getNickName());
        tmOperation.setOperatorPhone(contextTokenBean.getUserName());
        tmOperation.setOperatorCompanyId(contextTokenBean.getDepartmentId());
        tmOperation.setOperatorCompanyName(contextTokenBean.getDepartmentName());
        tmOperation.setOperatorRoleId(contextTokenBean.getRoleId());
        tmOperation.setOperatorRoleName(contextTokenBean.getRoleName());
        tmOperation.setOperateType(type);
        tmOperation.setOperateContent(content);
        tmOperation.setIsDelete(0);
        return getBaseMapper().insert(tmOperation);

    }
    public int insertNew(TmOperation tmOperation){

        if(null == tmOperation){
            return -1;
        }
        if(null == tmOperation.getId()){
            tmOperation.setId(SnowFlakeUUidUtils.generaUUid(null ,null , UuidPreEnum.OPERATION_ID_PRE.getPre()));
        }
        if(null == tmOperation.getCreateTime()){
            tmOperation.setCreateTime(LocalDateTime.now());
        }
        if(null == tmOperation.getUpdateTime()){
            tmOperation.setUpdateTime(LocalDateTime.now());
        }
        tmOperation.setIsDelete(0);
        return getBaseMapper().insert(tmOperation);
    }

    public IPage<TmOperation> getOperationList(Long pageNum , Long pageSize ,
                                               String startTime , String endTime,
                                               String operatorName, String companyId, String phone){
        if(null == pageNum){
            pageNum = 1L;
        }
        if(null == pageSize){
            pageSize = 1L;
        }
        IPage<TmOperation> page = new Page<>(pageNum , pageSize);
        LambdaQueryWrapper<TmOperation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNoneBlank(startTime , endTime)){
           LocalDateTime start = LocalDateTime.parse(startTime , DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
           LocalDateTime end = LocalDateTime.parse(endTime , DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
           if(null != start){
               lambdaQueryWrapper.ge(TmOperation::getOperateTime , start);
           }
           if(null != end){
               lambdaQueryWrapper.le(TmOperation::getOperateTime , end);
           }
        }
        if(StringUtils.isNotBlank(operatorName)){
            lambdaQueryWrapper.like(TmOperation::getOperatorName , operatorName);
        }
        if(StringUtils.isNotBlank(companyId)){
            lambdaQueryWrapper.like(TmOperation::getOperatorCompanyId , companyId);
        }
        if(StringUtils.isNotBlank(phone)){
            lambdaQueryWrapper.like(TmOperation::getOperatorPhone , phone);
        }
        lambdaQueryWrapper.eq(TmOperation::getIsDelete , 0);
        lambdaQueryWrapper.orderByDesc(TmOperation::getOperateTime);
        IPage<TmOperation> resultPage = getBaseMapper().selectPage(page , lambdaQueryWrapper);
        if(null == resultPage){
            log.info("获取操作列表的页数据失败");
            return page;
        }
        if(!CollectionUtils.isEmpty(resultPage.getRecords())){
            for (int i = 0; i < resultPage.getRecords().size(); i++) {
                String phoneNum = resultPage.getRecords().get(i).getOperatorPhone();
                if(StringUtils.isNotBlank(phoneNum) && phoneNum.length() >= 11){
                    StringBuilder resultPhoneNum = new StringBuilder();
                    resultPhoneNum.append(phoneNum.substring(0 , 3));
                    resultPhoneNum.append("****");
                    resultPhoneNum.append(phoneNum.substring(7));
                    resultPage.getRecords().get(i).setOperatorPhone(resultPhoneNum.toString());
                }
            }
        }
        return resultPage;
    }
}
