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

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.util.LoginUserUtil;
import com.storlead.framework.auth.vo.LoginUser;
import com.storlead.framework.common.result.Result;
import com.storlead.framework.mybatis.entity.SysBaseField;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import com.storlead.sales.pojo.dto.customer.CommonSearchDTO;
import com.storlead.sales.pojo.dto.customer.WorkOrderViewLogSearchDTO;
import com.storlead.sales.pojo.entity.customer.WorkOrderEntity;
import com.storlead.sales.pojo.entity.customer.WorkOrderViewLogEntity;
import com.storlead.sales.pojo.vo.customer.WorkOrderListVO;
import com.storlead.sales.pojo.vo.customer.WorkOrderViewLogListVO;
import com.storlead.sales.mapper.customer.WorkOrderViewLogMapper;
import com.storlead.sales.service.customer.WorkOrderViewLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 工单访问记录表 服务实现类
 * </p>
 *
 * @author YPZ
 * @since 2024-04-29
 */
@Service
public class WorkOrderViewLogServiceImpl extends MyBaseServiceImpl<WorkOrderViewLogMapper, WorkOrderViewLogEntity> implements WorkOrderViewLogService {

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Result<?> addWorkOrderViewLog(Long workOrderId) {

        LoginUser loginUser = LoginUserUtil.getLoginUser();
        //查询当前用户是否查看过该工单
        LambdaQueryWrapper<WorkOrderViewLogEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WorkOrderViewLogEntity::getWorkOrderId,workOrderId);
        wrapper.eq(WorkOrderViewLogEntity::getUserId,loginUser.getId());
        wrapper.eq(WorkOrderViewLogEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        wrapper.last("limit 1");
        WorkOrderViewLogEntity workOrderViewLog = this.getOne(wrapper);
        //如果存在查看的工单，浏览次数+1
        if (!ObjectUtils.isEmpty(workOrderViewLog)){
            workOrderViewLog.setViewCount(workOrderViewLog.getViewCount()+1);
            boolean success = this.updateById(workOrderViewLog);
            if (success){
                return Result.ok();
            }else {
                return Result.error("更新失败");
            }
        }
        //如果不存在，添加该用户查看的工单
        WorkOrderViewLogEntity workOrderViewLogEntity = new WorkOrderViewLogEntity();
        workOrderViewLogEntity.setWorkOrderId(workOrderId);
        workOrderViewLogEntity.setUserId(loginUser.getId());
        workOrderViewLogEntity.setViewCount(1);
        boolean success = this.save(workOrderViewLogEntity);
        if (!success){
            return Result.error("添加失败");
        }
        return Result.ok();
    }

    @Override
    public Result<IPage<WorkOrderViewLogListVO>> selectListVO(WorkOrderViewLogSearchDTO workOrderViewLogSearchDTO) {
        IPage<WorkOrderViewLogListVO> pageVO = new Page<>(workOrderViewLogSearchDTO.getPageIndex(),workOrderViewLogSearchDTO.getPageSize());
        IPage<WorkOrderViewLogEntity> page = new Page<>(workOrderViewLogSearchDTO.getPageIndex(),workOrderViewLogSearchDTO.getPageSize());

        LambdaQueryWrapper<WorkOrderViewLogEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(workOrderViewLogSearchDTO.getWorkOrderId()),WorkOrderViewLogEntity::getWorkOrderId, workOrderViewLogSearchDTO.getWorkOrderId());
        wrapper.eq(WorkOrderViewLogEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        page = page(page, wrapper);
        if (CollectionUtils.isEmpty(page.getRecords())){
            return Result.result(pageVO);
        }
//        List<CustomerListVO> customerListVO = new ArrayList<>();
        //复制list给VO类
        pageVO = SysBaseField.page(page,WorkOrderViewLogListVO.class);
        SysBaseField.dictFileds(pageVO.getRecords());
        SysBaseField.orgFileds(pageVO.getRecords());

        return Result.result(pageVO);
    }
}
