package com.gree.mall.manager.logic.material.manage;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.material.manage.WebsitWorkSettlementVO;
import com.gree.mall.manager.commonmapper.MaterialMapper;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.WebsitPartsSettlementRecord;
import com.gree.mall.manager.plus.entity.WebsitPartsSettlementRecordBak;
import com.gree.mall.manager.plus.service.WebsitPartsSettlementRecordBakService;
import com.gree.mall.manager.plus.service.WebsitPartsSettlementRecordService;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
@RequiredArgsConstructor
public class WebsitWorkSettlementLogic {

    private final CommonLogic commonLogic;
    private final MaterialMapper materialMapper;
    private final WebsitPartsSettlementRecordService websitPartsSettlementRecordService;
    private final WebsitPartsSettlementRecordBakService websitPartsSettlementRecordBakService;

    public IPage<WebsitWorkSettlementVO> pageList(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        FieldUtils.materialParam(zfireParamBean, WebsitWorkSettlementVO.class, adminUser);
        IPage<WebsitWorkSettlementVO> page = materialMapper.websitWorkSettlementPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
        return page;
    }

    public void del(List<String> ids) {
        List<WebsitPartsSettlementRecord> partsSettlementRecords = websitPartsSettlementRecordService.lambdaQuery()
                .in(WebsitPartsSettlementRecord::getPgId, ids)
                .list();

        websitPartsSettlementRecordService.lambdaUpdate()
                .in(WebsitPartsSettlementRecord::getPgId, ids)
                .remove();

        List<WebsitPartsSettlementRecordBak> partsSettlementRecordBakList = new ArrayList<>();
        for (WebsitPartsSettlementRecord partsSettlementRecord : partsSettlementRecords) {
            WebsitPartsSettlementRecordBak partsSettlementRecordBak = new WebsitPartsSettlementRecordBak();
            BeanUtils.copyProperties(partsSettlementRecord, partsSettlementRecordBak);
            partsSettlementRecordBakList.add(partsSettlementRecordBak);
        }
        websitPartsSettlementRecordBakService.saveBatch(partsSettlementRecordBakList);
    }
}
