package com.hwrj.cloud.portal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hwrj.cloud.portal.domain.UmsMemberOriginDto;
import com.hwrj.cloud.portal.mapper.UmsMemberOriginMapper;
import com.hwrj.cloud.portal.mapper.UmsMemberOriginProductMapper;
import com.hwrj.cloud.portal.model.UmsMemberOrigin;
import com.hwrj.cloud.portal.model.UmsMemberOriginExample;
import com.hwrj.cloud.portal.model.UmsMemberOriginProduct;
import com.hwrj.cloud.portal.model.UmsMemberOriginProductExample;
import com.hwrj.cloud.portal.service.UmsMemberOriginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UmsMemberOriginServiceImpl implements UmsMemberOriginService {

    @Autowired
    private UmsMemberOriginMapper umsMemberOriginMapper;

    @Autowired
    private UmsMemberOriginProductMapper umsMemberOriginProductMapper;
    @Override
    public UmsMemberOriginDto detail(Long id) {
        if (id == null){
            throw new RuntimeException("非法操作");
        }
        UmsMemberOriginDto dto = new UmsMemberOriginDto();
        UmsMemberOrigin umsMemberOrigin = umsMemberOriginMapper.selectByPrimaryKey(id);
        List<UmsMemberOriginProduct> umsMemberOriginProducts = productDetailListFromOriginId(id);
        BeanUtil.copyProperties(umsMemberOrigin,dto);
        dto.setProducts(umsMemberOriginProducts);
        return dto;
    }

    @Override
    public List<UmsMemberOriginDto> detailListOfCompany(Long companyId) {
        if (companyId == null){
            throw new RuntimeException("非法操作");
        }
        UmsMemberOriginExample example = new UmsMemberOriginExample();
        UmsMemberOriginExample.Criteria criteria = example.createCriteria();
        criteria.andCompIdEqualTo(companyId);
        List<UmsMemberOrigin> umsMemberOrigins = umsMemberOriginMapper.selectByExample(example);
        List<UmsMemberOriginDto> dtos = new ArrayList<>();
        umsMemberOrigins.stream().forEach(item->{
            UmsMemberOriginDto dto = new UmsMemberOriginDto();
            BeanUtil.copyProperties(item,dto);
            List<UmsMemberOriginProduct> products = productDetailListFromOriginId(item.getId());
            dto.setProducts(products);
            dtos.add(dto);

        });

        return dtos;
    }

    @Override
    public List<UmsMemberOriginProduct> productDetailListFromOriginId(Long originId) {
        UmsMemberOriginProductExample example = new UmsMemberOriginProductExample();
        UmsMemberOriginProductExample.Criteria criteria = example.createCriteria();
        criteria.andFkOriginIdEqualTo(originId);
        criteria.andStateEqualTo(0);
        List<UmsMemberOriginProduct> products = umsMemberOriginProductMapper.selectByExample(example);
        return products;
    }
}
