package com.gree.mall.miniapp.logic.material.parts;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.material.parts.PartsChangeSalesBean;
import com.gree.mall.miniapp.commonmapper.MaterialMapper;
import com.gree.mall.miniapp.enums.base.BaseEnum;
import com.gree.mall.miniapp.enums.material.PartsOrderFlagEnum;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.WebsitPartsChangeSales;
import com.gree.mall.miniapp.plus.entity.WebsitPartsChangeSalesItem;
import com.gree.mall.miniapp.plus.service.WebsitPartsChangeSalesItemService;
import com.gree.mall.miniapp.plus.service.WebsitPartsChangeSalesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class WebsitPartsChangeSalesLogic {

    @Resource
    MaterialMapper materialMapper;
    @Resource
    CommonLogic commonLogic;
    @Resource
    WebsitPartsChangeSalesService websitPartsChangeSalesService;
    @Resource
    WebsitPartsChangeSalesItemService websitPartsChangeSalesItemService;

    public Object appList(String changeSalesNo, String flag, String identity, Integer pageNo, Integer pageSize) {
        Page page = websitPartsChangeSalesService.lambdaQuery()
                .eq(StringUtils.isNotBlank(changeSalesNo), WebsitPartsChangeSales::getChangeSalesNo, changeSalesNo)
                .eq(StringUtils.isNotBlank(flag), WebsitPartsChangeSales::getFlag, flag)
                .eq(WebsitPartsChangeSales::getIdentity, identity)
                .orderByDesc(WebsitPartsChangeSales::getCreateTime)
                .page(new Page<>(pageNo, pageSize));

        List records = page.getRecords();
        List<PartsChangeSalesBean> partsChangeSalesBeanList = new ArrayList<>();
        for (Object record : records) {
            WebsitPartsChangeSales partsChangeSales = (WebsitPartsChangeSales) record;
            PartsChangeSalesBean partsChangeSalesBean = new PartsChangeSalesBean();
            BeanUtils.copyProperties(partsChangeSales, partsChangeSalesBean);

            partsChangeSalesBean.setFlagName(BaseEnum.keyToEnumNotNull(PartsOrderFlagEnum.class, partsChangeSalesBean.getFlag()).getRemark());

            List<WebsitPartsChangeSalesItem> partsNewRefundManageItems = websitPartsChangeSalesItemService.lambdaQuery()
                    .eq(WebsitPartsChangeSalesItem::getChangeSalesNo, changeSalesNo)
                    .list();

            partsChangeSalesBean.setPartsChangeSalesItemList(partsNewRefundManageItems);
            partsChangeSalesBeanList.add(partsChangeSalesBean);
        }
        page.setRecords(partsChangeSalesBeanList);
        return page;
    }

    public Object detail(String changeSalesNo) {
        WebsitPartsChangeSales partsChangeSales = websitPartsChangeSalesService.lambdaQuery()
                .eq(WebsitPartsChangeSales::getChangeSalesNo, changeSalesNo)
                .one();
        PartsChangeSalesBean partsChangeSalesBean = new PartsChangeSalesBean();
        BeanUtils.copyProperties(partsChangeSales, partsChangeSalesBean);
        partsChangeSalesBean.setFlagName(BaseEnum.keyToEnumNotNull(PartsOrderFlagEnum.class, partsChangeSalesBean.getFlag()).getRemark());
        List<WebsitPartsChangeSalesItem> partsChangeSalesItems = websitPartsChangeSalesItemService.lambdaQuery()
                .eq(WebsitPartsChangeSalesItem::getChangeSalesNo, changeSalesNo)
                .list();
        partsChangeSalesBean.setPartsChangeSalesItemList(partsChangeSalesItems);
        return partsChangeSalesBean;
    }
}
