package com.diagbot.facade;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diagbot.client.UserServiceClient;
import com.diagbot.dto.RespDTO;
import com.diagbot.dto.UserOrgDTO;
import com.diagbot.entity.LantoneProduct;
import com.diagbot.entity.OpenedProductsIndex;
import com.diagbot.entity.wrapper.LantoneProductWrapper;
import com.diagbot.entity.wrapper.OpendProductWrapper;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.service.impl.LantoneProductServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.DateUtil;
import com.diagbot.util.UserUtils;
import com.diagbot.vo.OppendedProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @Description:产品业务层
 * @author: wangyu
 * @time: 2018/9/19 15:52
 */
@Component
public class LantoneProductFacade extends LantoneProductServiceImpl {
    @Autowired
    private OpenedProductsFacade openedProductsFacade;
    @Autowired
    private UserServiceClient userServiceClient;

    /**
     * @Description: 添加产品业务逻辑
     * @Author: wangyu
     * @Date: 19:59 2018/9/18
     */
    public Boolean addProducts(LantoneProduct lantoneProduct) {
        lantoneProduct.setGmtCreate(DateUtil.now());
        lantoneProduct.setCreator(UserUtils.getCurrentPrincipleID());
        if (!save(lantoneProduct)) {
            throw new CommonException(CommonErrorCode.FAIL);
        }
        return true;
    }

    /**
     * @Description: 修改产品业务逻辑
     * @Author: wangyu
     * @Date: 19:59 2018/9/18
     */
    public Boolean updateProduct(LantoneProduct lantoneProduct) {
        if (!updateById(lantoneProduct)) {
            throw new CommonException(CommonErrorCode.FAIL);
        }
        return true;
    }

    /**
     * @Description: 删除产品业务逻辑
     * @Author: wangyu
     * @Date: 19:59 2018/9/18
     */
    public Boolean deleteProduct(LantoneProduct lantoneProduct) {
        lantoneProduct.setIsDeleted("Y");
        if (!updateById(lantoneProduct)) {
            throw new CommonException(CommonErrorCode.FAIL);
        }
        return true;
    }

    /**
     * @Description: 更改产品状态业务逻辑
     * @Author: wangyu
     * @Date: 19:59 2018/9/18
     */
    public Boolean productStatus(LantoneProduct lantoneProduct) {
        if (!updateById(lantoneProduct)) {
            throw new CommonException(CommonErrorCode.FAIL);
        }
        return true;
    }

    /**
     * @Description: 查询当前用户已开通产品
     * @Author: zhaops
     * @Date: 10:10 2018/9/20
     */
    public List<LantoneProductWrapper> opendedProductByCurrentUser() {
        Long userId=Long.parseLong(UserUtils.getCurrentPrincipleID());
        return this.opendedProductByUserId(userId);
    }

    /**
     * @Description: 根据用户id查询是否有开通产品
     * @Author: wangyu
     * @Date: 19:59 2018/9/18
     */
    public IPage<LantoneProduct> productLine(Page page){
        Long userId = Long.parseLong(UserUtils.getCurrentPrincipleID());
        IPage<LantoneProduct> diagLantoneProducts =productLine(page,userId);
        return diagLantoneProducts;
    }

    /**
     * @Description: 根据产品id查询产品
     * @Author: wangyu
     * @Date: 17:31 2018/9/20
     */
    public LantoneProduct selectLanProduct(Long id){
        return getById(id);
    }

    /**
     * @Description: 根据产品id查询当条产品线下所有订单信息
     * @Author: wangyu
     * @Date: 10:15 2018/9/21
     */
    public List<OpendProductWrapper> opendedProduct(OppendedProductVO oppendedProductVO){
        OpenedProductsIndex openedProductsIndex =new OpenedProductsIndex();
        Long curren = oppendedProductVO.getCurrent();
        Long size =oppendedProductVO.getSize();
        if(curren==null){
            oppendedProductVO.setCurrent(1L);
        }
        if (size==null){
            oppendedProductVO.setSize(10L);
        }
        BeanUtil.copyProperties(oppendedProductVO, openedProductsIndex);
        openedProductsIndex.setIndex((oppendedProductVO.getCurrent().longValue()-1)*oppendedProductVO.getSize().longValue());
        List<OpendProductWrapper> list = openedProductsFacade.getByProductId(openedProductsIndex);
        List<Long> list1 =new ArrayList<>();
        for (OpendProductWrapper opendProducts:list) {
            Long longs = opendProducts.getUserId();
            list1.add(longs);
        }
        RespDTO<Map<Long, UserOrgDTO>> mapRespDTO = userServiceClient.getUserAndOrg(list1);
        if(mapRespDTO == null || !"0".equals(mapRespDTO.code) ) {
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,
                    "获取用户和机构信息失败");
        }
        Map<Long, UserOrgDTO> dataMap = mapRespDTO.data;
        for(OpendProductWrapper bean : list) {
            UserOrgDTO uo = dataMap.get(bean.getUserId());
            if(uo != null) {
                bean.setUsername(uo.getUsername());
                bean.setLinkman(uo.getLinkman());
                bean.setEmail(uo.getEmail());
                bean.setOrgId(uo.getOrgId());
                bean.setOrgName(uo.getOrgName());
                bean.setIsReject(uo.getIsReject());
                bean.setAuStatus(uo.getAuStatus());
            }
        }
        return list;
    }
}
