package com.miaxis.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.customer.domain.CustomerInfo;
import com.miaxis.customer.dto.AppletCustomerInfoDto;
import com.miaxis.customer.mapper.CustomerInfoMapper;
import com.miaxis.customer.service.ICustomerInfoService;
import com.miaxis.customer.vo.CustomerInfoVo;
import com.miaxis.customer.vo.ExhibitionCustomerInfoVo;
import com.miaxis.product.domain.ProductTypeInfo;
import com.miaxis.product.mapper.ProductTypeInfoMapper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户信息Service业务层处理
 *
 * @author miaxis
 * @date 2021-03-10
 */
@Service
@RequiredArgsConstructor
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements ICustomerInfoService {

    private final CustomerInfoMapper customerInfoMapper;

    private final ProductTypeInfoMapper productTypeInfoMapper;


    /**
     * 查询客户信息列表
     *
     * @param customerInfo 客户信息
     * @return 客户信息
     */
    @Override
    public List<CustomerInfoVo> selectCustomerInfoList(CustomerInfo customerInfo){
        return customerInfoMapper.selectCustomerInfoList(customerInfo);
    }

    /**
     * 更新 0：上架 / 1：下架
     * @param ids
     * @param shelfStatus
     * @return
     */
    @Override
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public Response updateShelfStatus(Long[] ids, Integer shelfStatus) {
        for (Long id : ids) {
            this.update(new UpdateWrapper<CustomerInfo>().set("shelf_status",shelfStatus).eq("id",id));
        }
        return Response.success();
    }

    /**
     * 删除客户信息(伪删除)
     * @param ids
     * @return
     */
    @Override
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public Response removeCustomerByIds(Long[] ids) {
        for (Long id : ids) {
            this.update(new UpdateWrapper<CustomerInfo>().set("status",1).eq("id",id));
        }
        return Response.success();
    }

    /**
     * 获取客户信息详细信息
     * @param id
     * @return
     */
    @Override
    @SneakyThrows
    public Response<CustomerInfoVo> getCustomerById(Long id) {
        return Response.success(customerInfoMapper.getCustomerById(id));
    }

    /**
     * applet
     * 查询商家列表
     * @param appletCustomerInfoDto
     * @param userId
     * @return
     */
    @Override
    public List<ExhibitionCustomerInfoVo> getCustomerListByProductId(AppletCustomerInfoDto appletCustomerInfoDto, Long userId) {

        return customerInfoMapper.getCustomerListByProductId(userId,appletCustomerInfoDto);
    }



}
