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

import cn.hutool.core.bean.BeanUtil;
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.coupon.CouponPageBeanVO;
import com.gree.mall.manager.bean.listvo.workorder.OrderBaseVO;
import com.gree.mall.manager.bean.notice.NoticeRecordReadBean;
import com.gree.mall.manager.bean.workorder.IncreAddOrUp;
import com.gree.mall.manager.bean.workorder.IncreVO;
import com.gree.mall.manager.bean.workorder.PgIncreVO;
import com.gree.mall.manager.commonmapper.workorder.IncreMapper;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.AdminWebsit;
import com.gree.mall.manager.plus.entity.PgIncre;
import com.gree.mall.manager.plus.entity.PgIncreItem;
import com.gree.mall.manager.plus.entity.PgIncreWebsit;
import com.gree.mall.manager.plus.service.AdminWebsitService;
import com.gree.mall.manager.plus.service.PgIncreItemService;
import com.gree.mall.manager.plus.service.PgIncreService;
import com.gree.mall.manager.plus.service.PgIncreWebsitService;
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.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class IncreConfigLogic {

    @Autowired
    IncreMapper increMapper;

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    PgIncreItemService pgIncreItemService;

    @Autowired
    PgIncreService pgIncreService;

    @Autowired
    PgIncreWebsitService pgIncreWebsitService;

    @Autowired
    AdminWebsitService adminWebsitService;

    public IPage<IncreVO> list(Page page, ZfireParamBean zfireParam) {
        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        //1.组装查询条件
        zfireParam = FieldUtils.supplyParam(zfireParam, IncreVO.class);

        IPage<IncreVO> increVOIPage = increMapper.list(page, zfireParam,adminCompanyIds);
        return increVOIPage;
    }

    @Transactional(rollbackFor = Exception.class)
    public void add(IncreAddOrUp increAddOrUp) {
        increAddOrUp.setNum(increAddOrUp.getPgIncreItems().size());
        increAddOrUp.insert();
        for (PgIncreItem pgIncreItem : increAddOrUp.getPgIncreItems()) {
            pgIncreItem.setIncreId(increAddOrUp.getId());
        }

        for (PgIncreWebsit pgIncreWebsit : increAddOrUp.getPgIncreWebsits()) {
            AdminWebsit adminWebsit = adminWebsitService.getById(pgIncreWebsit.getWebsitId());
            pgIncreWebsit.setPgIncreId(increAddOrUp.getId());
            pgIncreWebsit.setWebsitName(adminWebsit.getName());
        }



        pgIncreWebsitService.saveBatch(increAddOrUp.getPgIncreWebsits());

        pgIncreItemService.saveBatch(increAddOrUp.getPgIncreItems());
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(IncreAddOrUp increAddOrUp) {
        increAddOrUp.setNum(increAddOrUp.getPgIncreItems().size());
        increAddOrUp.updateById();
        pgIncreItemService.lambdaUpdate().eq(PgIncreItem::getIncreId,increAddOrUp.getId()).remove();
        pgIncreWebsitService.lambdaUpdate().eq(PgIncreWebsit::getPgIncreId,increAddOrUp.getId()).remove();
        for (PgIncreItem pgIncreItem : increAddOrUp.getPgIncreItems()) {
            pgIncreItem.setIncreId(increAddOrUp.getId());
        }

        for (PgIncreWebsit pgIncreWebsit : increAddOrUp.getPgIncreWebsits()) {
            AdminWebsit adminWebsit = adminWebsitService.getById(pgIncreWebsit.getWebsitId());
            pgIncreWebsit.setPgIncreId(increAddOrUp.getId());
            pgIncreWebsit.setWebsitName(adminWebsit.getName());
        }

        pgIncreWebsitService.saveBatch(increAddOrUp.getPgIncreWebsits());

        pgIncreItemService.saveBatch(increAddOrUp.getPgIncreItems());
    }

    @Transactional(rollbackFor = Exception.class)
    public void upOrDown(IncreAddOrUp increAddOrUp) {
        increAddOrUp.updateById();
    }

    public PgIncreVO detail(String id) {
        PgIncre pgIncre = pgIncreService.getById(id);

        PgIncreVO pgIncreVO = BeanUtil.toBean(pgIncre, PgIncreVO.class);

        List<PgIncreItem> list = pgIncreItemService.lambdaQuery()
                .eq(PgIncreItem::getIncreId, id).list();
        pgIncreVO.setPgIncreItems(list);

        List<PgIncreWebsit> pgIncreWebsits = pgIncreWebsitService.lambdaQuery()
                .eq(PgIncreWebsit::getPgIncreId, id).list();

        pgIncreVO.setPgIncreWebsits(pgIncreWebsits);

        return pgIncreVO;
    }
}
