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.google.gson.Gson;
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.sales.pojo.dto.customer.CustomerOwnerRecordSearchDTO;
import com.storlead.sales.pojo.entity.customer.CustomerCompanyEntity;
import com.storlead.sales.pojo.entity.customer.CustomerEntity;
import com.storlead.sales.pojo.entity.customer.CustomerOwnerRecordEntity;
import com.storlead.sales.pojo.vo.customer.CustomerAllVO;
import com.storlead.sales.pojo.vo.customer.CustomerListVO;
import com.storlead.sales.pojo.vo.customer.CustomerOwnerRecordListVO;
import com.storlead.sales.mapper.customer.CustomerOwnerRecordMapper;
import com.storlead.sales.service.customer.CustomerCompanyService;
import com.storlead.sales.service.customer.CustomerOwnerRecordService;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import com.storlead.sales.service.customer.CustomerService;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户归属人变更记录表 服务实现类
 * </p>
 *
 * @author YPZ
 * @since 2024-11-05
 */
@Service
public class CustomerOwnerRecordServiceImpl extends MyBaseServiceImpl<CustomerOwnerRecordMapper, CustomerOwnerRecordEntity> implements CustomerOwnerRecordService {

    @Resource
    private CustomerCompanyService customerCompanyService;
    @Override
    public Result<IPage<CustomerOwnerRecordListVO>> selectListVO(CustomerOwnerRecordSearchDTO customerOwnerRecordSearchDTO) {
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        IPage<CustomerOwnerRecordListVO> pageVO =new Page<>(customerOwnerRecordSearchDTO.getPageIndex(), customerOwnerRecordSearchDTO.getPageSize());

        IPage<CustomerOwnerRecordEntity> page = new Page<>(customerOwnerRecordSearchDTO.getPageIndex(), customerOwnerRecordSearchDTO.getPageSize());

        LambdaQueryWrapper<CustomerOwnerRecordEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq( CustomerOwnerRecordEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        wrapper.eq(!ObjectUtils.isEmpty(customerOwnerRecordSearchDTO.getCustomerId()), CustomerOwnerRecordEntity::getCustomerId, customerOwnerRecordSearchDTO.getCustomerId());
//        page = pageWithScope(page, wrapper,customerOwnerRecordSearchDTO,"");
        page = page(page, wrapper);
        if (CollectionUtils.isEmpty(page.getRecords())){
            return Result.result(pageVO);
        }
        DefaultMapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        List<CustomerOwnerRecordListVO> customerOwnerRecordListVO = mapperFactory.getMapperFacade().mapAsList(page.getRecords(),CustomerOwnerRecordListVO.class);
//        List<Long> customerIdList = customerOwnerRecordListVO.stream().map(CustomerOwnerRecordListVO::getCustomerId).collect(Collectors.toList());
//        Map<Long, CustomerCompanyEntity> customerCompanyMap = customerCompanyService.customerIdListToCompanyMap(customerIdList);
        for (CustomerOwnerRecordListVO customerOwnerRecordListVO1:customerOwnerRecordListVO){
            if (!ObjectUtils.isEmpty(customerOwnerRecordListVO1.getCustomerDetail())){
                Gson gson = new Gson();
                // 将JSON字符串转换回CustomerEntity对象
                CustomerAllVO customerAllVO = gson.fromJson(customerOwnerRecordListVO1.getCustomerDetail(), CustomerAllVO.class);

                if (!ObjectUtils.isEmpty(customerAllVO.getCustomerCompany())){
                    customerOwnerRecordListVO1.setIndustry(customerAllVO.getCustomerCompany().getIndustry());
                }
//                customerOwnerRecordListVO1.setCustomer(customerAllVO);
                customerOwnerRecordListVO1.setCustomerTypeDictValue(customerAllVO.getCustomerTypeDictValue());
                customerOwnerRecordListVO1.setCustomerStatusDictValue(customerAllVO.getCustomerStatusDictValue());
                customerOwnerRecordListVO1.setCustomerDetail(null);
            }
        }
        pageVO.setTotal(page.getTotal());
        pageVO.setPages(page.getPages());
        pageVO.setRecords(customerOwnerRecordListVO);
        pageVO.setCurrent(page.getCurrent());
        SysBaseField.dictFileds(pageVO.getRecords());
        SysBaseField.orgFileds(pageVO.getRecords());

        return Result.result(pageVO);

    }
}
