package com.storlead.sales.service.impl.systemrule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.framework.common.util.HtmlUtils;
import com.storlead.framework.common.result.Result;
import com.storlead.framework.mybatis.entity.SysBaseField;
import com.storlead.sales.pojo.entity.customer.CustomerEntity;
import com.storlead.sales.pojo.entity.customer.CustomerFollowUpEntity;
import com.storlead.sales.pojo.vo.customer.CustomerFollowUpLastVO;
import com.storlead.sales.enums.ResourceTypeEnum;
import com.storlead.sales.pojo.dto.systemrule.OperationLogSearchDTO;
import com.storlead.sales.pojo.entity.systemrule.OperationLogEntity;
import com.storlead.sales.pojo.vo.systemrule.OperationLogListVO;
import com.storlead.sales.mapper.systemrule.OperationLogMapper;
import com.storlead.sales.service.systemrule.OperationLogService;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import com.storlead.user.pojo.entity.JobEntity;
import com.storlead.user.pojo.entity.UserEntity;
import com.storlead.user.service.IJobService;
import com.storlead.user.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 操作日志表 服务实现类
 * </p>
 *
 * @author YPZ
 * @since 2024-06-18
 */
@Service
public class OperationLogServiceImpl extends MyBaseServiceImpl<OperationLogMapper, OperationLogEntity> implements OperationLogService {

    @Resource
    private IUserService userService;
    @Resource
    private IJobService jobService;
    @Override
    public void logOperation(String tableName,Integer operationType, String resourceType, Long resourceTypeId, String fieldName,String swaggerTitle, String oldValue, String newValue, String operator) {
        OperationLogEntity log = new OperationLogEntity();
        log.setTableName(tableName);
        log.setOperationType(operationType);
        log.setResourceType(resourceType);
        log.setResourceTypeId(resourceTypeId);
        log.setFieldName(fieldName);
        log.setSwaggerTitle(swaggerTitle);
        log.setOldValue(HtmlUtils.clearContent(HtmlUtils.clearContent(oldValue)));
        log.setNewValue(HtmlUtils.clearContent(HtmlUtils.clearContent(newValue)));
        log.setOperator(operator);
        save(log);
    }

    @Override
    public Result<IPage<OperationLogListVO>> selectListVO(OperationLogSearchDTO operationLogSearchDTO) {
        //修改默认安装id排序
        if (ObjectUtils.isEmpty(operationLogSearchDTO.getSortMethod())) {
            operationLogSearchDTO.setSortMethod("desc");
        }
        if (ObjectUtils.isEmpty(operationLogSearchDTO.getCommonQueryOrderBy())){
            operationLogSearchDTO.setCommonQueryOrderBy("id");
        }
        IPage<OperationLogListVO> pageVO = new Page<>(operationLogSearchDTO.getPageIndex(), operationLogSearchDTO.getPageSize());
        IPage<OperationLogEntity> page = new Page<>(operationLogSearchDTO.getPageIndex(), operationLogSearchDTO.getPageSize());
        LambdaQueryWrapper<OperationLogEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OperationLogEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        wrapper.ge(!ObjectUtils.isEmpty(operationLogSearchDTO.getBeginTime()), OperationLogEntity::getOperationTime,operationLogSearchDTO.getBeginTime());
        wrapper.le(!ObjectUtils.isEmpty(operationLogSearchDTO.getEndTime()), OperationLogEntity::getOperationTime,operationLogSearchDTO.getEndTime());
        wrapper.eq(!ObjectUtils.isEmpty(operationLogSearchDTO.getOperationType()),OperationLogEntity::getOperationType, operationLogSearchDTO.getOperationType());
        wrapper.eq(!ObjectUtils.isEmpty(operationLogSearchDTO.getResourceType()),OperationLogEntity::getResourceType, operationLogSearchDTO.getResourceType());
        wrapper.eq(!ObjectUtils.isEmpty(operationLogSearchDTO.getResourceTypeId()),OperationLogEntity::getResourceTypeId, operationLogSearchDTO.getResourceTypeId());
        page = pageWithScope(page, wrapper,operationLogSearchDTO,"");
        if (CollectionUtils.isEmpty(page.getRecords())){
            return Result.result(pageVO);
        }
//        List<CustomerListVO> customerListVO = new ArrayList<>();
        //复制list给VO类
        pageVO = SysBaseField.page(page, OperationLogListVO.class);
        List<Long> createByList = page.getRecords().stream().map(OperationLogEntity::getCreateBy).collect(Collectors.toList());
        //获取创建人信息
        Map<Long, UserEntity> userMap = userService.IdListToUserMap(createByList);
        Collection<UserEntity> users = userService.listByIds(createByList);
        Map<Long, JobEntity> jobMap = jobService.getJobMap(users);

        for (OperationLogListVO operationLogListVO : pageVO.getRecords()){
            //添加创建人头像
            UserEntity userEntity = userMap.get(operationLogListVO.getCreateBy());
            operationLogListVO.setCreateByAvatar(userEntity.getAvatar());
            if (Integer.valueOf(1).equals(operationLogListVO.getOperationType())) {
                operationLogListVO.setOperationTypeName("新增");
            } else if (Integer.valueOf(2).equals(operationLogListVO.getOperationType())) {
                operationLogListVO.setOperationTypeName("修改");
            } else if (Integer.valueOf(3).equals(operationLogListVO.getOperationType())) {
                operationLogListVO.setOperationTypeName("删除");
            } else {
                operationLogListVO.setOperationTypeName("其他");
            }
            JobEntity job = jobMap.get(userEntity.getJobId());
            if (Objects.nonNull(job)) {
                operationLogListVO.setOwnerByJobName(job.getName());
            }
        }
        SysBaseField.dictFileds(pageVO.getRecords());
        SysBaseField.orgFileds(pageVO.getRecords());
        return Result.result(pageVO);
    }
}
