package com.gree.mall.manager.logic.trade;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.order.OldProductManagerAdd;
import com.gree.mall.manager.bean.order.OldProductManagerItemAdd;
import com.gree.mall.manager.bean.order.OrderOldProductAdd;
import com.gree.mall.manager.bean.trade.GoodsCategoryItemVO;
import com.gree.mall.manager.bean.trade.OldProductManagerDetail;
import com.gree.mall.manager.bean.trade.OldProductManagerVO;
import com.gree.mall.manager.commonmapper.JxMapper;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.plus.service.GoodsCategoryItemService;
import com.gree.mall.manager.plus.service.OldProductManagerItemAttributeService;
import com.gree.mall.manager.plus.service.OldProductManagerItemService;
import com.gree.mall.manager.plus.service.OldProductManagerService;
import com.gree.mall.manager.utils.StringUtil;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class OldProdctManagerLogic {

    @Autowired
    JxMapper jxMapper;

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    GoodsCategoryItemService goodsCategoryItemService;

    @Autowired
    OldProductManagerService oldProductManagerService;

    @Autowired
    OldProductManagerItemService oldProductManagerItemService;

    @Autowired
    OldProductManagerItemAttributeService oldProductManagerItemAttributeService;

    public IPage<OldProductManagerVO> pageV2(ZfireParamBean zfireParamBean) {

        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> companyWechatIds = adminUser.getCompanyWechatIds();

        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean, OldProductManagerVO.class);

        IPage<OldProductManagerVO> oldProductManagerVOIPage = jxMapper.pageManagerV2(new Page(zfireParamBean.getPageNum(),
                        zfireParamBean.getPageSize()),
                zfireParam
                , companyWechatIds);

        return oldProductManagerVOIPage;
    }

    @Transactional(rollbackFor = Exception.class)
    public void addItem(OldProductManagerAdd oldProductManagerAdd) {

        AdminUserCom adminUser = commonLogic.getAdminUser();

        oldProductManagerAdd.setCompanyWechatId(adminUser.getCompanyWechatId());
        oldProductManagerAdd.setCompanyName(adminUser.getCompanyName());

        oldProductManagerAdd.insert();

        //旧机回收

        if (!CollectionUtils.isEmpty(oldProductManagerAdd.getOrderOldProductAdds())) {

            List<OldProductManagerItem> oldProductManagerItems = new ArrayList<>();
            List<OldProductManagerItemAttribute> oldProductManagerItemAttributes = new ArrayList<>();

            for (OrderOldProductAdd orderOldProductDTO : oldProductManagerAdd.getOrderOldProductAdds()) {

                GoodsCategoryItem goodsCategoryItemDto = goodsCategoryItemService.getById(orderOldProductDTO.getId());

                OldProductManagerItem oldProductManagerItem = BeanUtil.toBean(goodsCategoryItemDto, OldProductManagerItem.class);


                oldProductManagerItem.setId(IdWorker.getIdStr());
                oldProductManagerItem.setNum(orderOldProductDTO.getNum());
                oldProductManagerItem.setPayAmount(orderOldProductDTO.getPayAmount());
                oldProductManagerItem.setSpecId(goodsCategoryItemDto.getId());
                oldProductManagerItem.setSpecName(goodsCategoryItemDto.getDictName());
                oldProductManagerItem.setOldProductManagerId(oldProductManagerAdd.getId());

                for (String goodsCategoryItem : orderOldProductDTO.getItemIds()) {

                    GoodsCategoryItem goodsCategoryItems = goodsCategoryItemService.getById(goodsCategoryItem);
                    OldProductManagerItemAttribute oldProductManagerItemAttribute = BeanUtil.toBean(goodsCategoryItems, OldProductManagerItemAttribute.class);

                    oldProductManagerItemAttribute.setOldProductManagerItemId(oldProductManagerItem.getId());
                    oldProductManagerItemAttribute.setOldProductManagerId(oldProductManagerAdd.getId());
                    oldProductManagerItemAttribute.setGoodsCategoryItemId(goodsCategoryItems.getId());

                    oldProductManagerItem.setItemName((StringUtil.isEmpty(oldProductManagerItem.getItemName())?"":oldProductManagerItem.getItemName())+goodsCategoryItems.getDictName()+",");


                    oldProductManagerItemAttributes.add(oldProductManagerItemAttribute);

                }

                if (!StringUtil.isEmpty(oldProductManagerItem.getItemName())){
                    String substring = oldProductManagerItem.getItemName().substring(0, oldProductManagerItem.getItemName().length() - 1);
                    oldProductManagerItem.setItemName(substring);
                }


                oldProductManagerItems.add(oldProductManagerItem);


            }


            if (!CollectionUtils.isEmpty(oldProductManagerItems)) {
                oldProductManagerItemService.saveBatch(oldProductManagerItems);
            }

            if (!CollectionUtils.isEmpty(oldProductManagerItemAttributes)) {
                oldProductManagerItemAttributeService.saveBatch(oldProductManagerItemAttributes);
            }


        }

    }

    @Transactional(rollbackFor = Exception.class)
    public void update(OldProductManagerAdd oldProductManagerAdd, HttpServletRequest request) {
        oldProductManagerAdd.updateById();

        //旧机回收

        oldProductManagerItemService.lambdaUpdate().eq(OldProductManagerItem::getOldProductManagerId,oldProductManagerAdd.getId()).remove();
        oldProductManagerItemAttributeService.lambdaUpdate().eq(OldProductManagerItemAttribute::getOldProductManagerId,oldProductManagerAdd.getId()).remove();

        if (!CollectionUtils.isEmpty(oldProductManagerAdd.getOrderOldProductAdds())) {

            List<OldProductManagerItem> oldProductManagerItems = new ArrayList<>();
            List<OldProductManagerItemAttribute> oldProductManagerItemAttributes = new ArrayList<>();

            for (OrderOldProductAdd orderOldProductDTO : oldProductManagerAdd.getOrderOldProductAdds()) {

                GoodsCategoryItem goodsCategoryItemDto = goodsCategoryItemService.getById(orderOldProductDTO.getId());

                OldProductManagerItem oldProductManagerItem = BeanUtil.toBean(goodsCategoryItemDto, OldProductManagerItem.class);


                oldProductManagerItem.setId(IdWorker.getIdStr());
                oldProductManagerItem.setNum(orderOldProductDTO.getNum());
                oldProductManagerItem.setPayAmount(orderOldProductDTO.getPayAmount());
                oldProductManagerItem.setSpecId(goodsCategoryItemDto.getId());
                oldProductManagerItem.setSpecName(goodsCategoryItemDto.getDictName());
                oldProductManagerItem.setOldProductManagerId(oldProductManagerAdd.getId());

                for (String goodsCategoryItem : orderOldProductDTO.getItemIds()) {

                    GoodsCategoryItem goodsCategoryItems = goodsCategoryItemService.getById(goodsCategoryItem);
                    OldProductManagerItemAttribute oldProductManagerItemAttribute = BeanUtil.toBean(goodsCategoryItems, OldProductManagerItemAttribute.class);
                    oldProductManagerItemAttribute.setId(null);
                    oldProductManagerItemAttribute.setGoodsCategoryItemId(goodsCategoryItems.getId());

                    oldProductManagerItemAttribute.setOldProductManagerItemId(oldProductManagerItem.getId());

                    oldProductManagerItem.setItemName((StringUtil.isEmpty(oldProductManagerItem.getItemName())?"":oldProductManagerItem.getItemName())+goodsCategoryItems.getDictName()+",");

                    oldProductManagerItemAttributes.add(oldProductManagerItemAttribute);

                }

                if (!StringUtil.isEmpty(oldProductManagerItem.getItemName())){
                    String substring = oldProductManagerItem.getItemName().substring(0, oldProductManagerItem.getItemName().length() - 1);
                    oldProductManagerItem.setItemName(substring);
                }


                oldProductManagerItems.add(oldProductManagerItem);


            }


            if (!CollectionUtils.isEmpty(oldProductManagerItems)) {
                oldProductManagerItemService.saveBatch(oldProductManagerItems);
            }

            if (!CollectionUtils.isEmpty(oldProductManagerItemAttributes)) {
                oldProductManagerItemAttributeService.saveBatch(oldProductManagerItemAttributes);
            }


        }

    }

    public void del(String oldManagerId) {

        oldProductManagerService.removeById(oldManagerId);

        oldProductManagerItemService.lambdaUpdate().eq(OldProductManagerItem::getOldProductManagerId,oldManagerId).remove();
        oldProductManagerItemAttributeService.lambdaUpdate().eq(OldProductManagerItemAttribute::getOldProductManagerId,oldManagerId).remove();

    }

    public void out(List<String> oldManagerId) {
        oldProductManagerService.lambdaUpdate().in(OldProductManager::getId,oldManagerId)
                .set(OldProductManager::getStatus,"OUT").update();
    }

    public OldProductManagerDetail detail(String oldManagerId) {
        OldProductManager oldProductManager = oldProductManagerService.getById(oldManagerId);

        OldProductManagerDetail oldProductManagerDetail = BeanUtil.toBean(oldProductManager, OldProductManagerDetail.class);

        List<OldProductManagerItem> list = oldProductManagerItemService.lambdaQuery().eq(OldProductManagerItem::getOldProductManagerId, oldProductManagerDetail.getId())
                .list();

        List<OldProductManagerItemAdd> oldProductManagerItemAdds = new ArrayList<>();

        for (OldProductManagerItem oldProductManagerItem : list) {
            OldProductManagerItemAdd oldProductManagerItemAdd = BeanUtil.toBean(oldProductManagerItem, OldProductManagerItemAdd.class);

            List<OldProductManagerItemAttribute> oldProductManagerItemAttributes = oldProductManagerItemAttributeService.lambdaQuery()
                    .eq(OldProductManagerItemAttribute::getOldProductManagerItemId, oldProductManagerItemAdd.getId()).list();
            oldProductManagerItemAdd.setOldProductManagerItemAttributes(oldProductManagerItemAttributes);

            oldProductManagerItemAdds.add(oldProductManagerItemAdd);

        }

        oldProductManagerDetail.setItemList(oldProductManagerItemAdds);
        return oldProductManagerDetail;
    }
}
