package com.miaxis.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaxis.common.core.domain.Response;
import com.miaxis.customer.domain.CustomerInfo;
import com.miaxis.customer.mapper.CustomerInfoMapper;
import com.miaxis.customer.service.ICustomerInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

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

    private final CustomerInfoMapper customerInfoMapper;

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

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

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