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

import cn.hutool.core.collection.CollectionUtil;
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.vender.WebsitVenderVO;
import com.gree.mall.manager.commonmapper.MaterialMapper;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.WebsitVender;
import com.gree.mall.manager.plus.service.WebsitVenderService;
import com.gree.mall.manager.zfire.bean.QueryParamBean;
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.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Objects;

@Slf4j
@Service
@RequiredArgsConstructor
public class VenderLogic {

    private final CommonLogic commonLogic;
    private final MaterialMapper materialMapper;
    private final WebsitVenderService websitVenderService;

    public IPage<WebsitVenderVO> page(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        if (CollectionUtil.isEmpty(zfireParamBean.getParams())) {
            zfireParamBean.setParams(new ArrayList<>());
        }

        if (adminUser.getIsVender()) {
            QueryParamBean paramBean = new QueryParamBean();
            paramBean.setParam("a.vender_id").setCompare("=").setValue(adminUser.getVenderId());
            zfireParamBean.getParams().add(paramBean);
        }
        FieldUtils.materialParam(zfireParamBean, WebsitVenderVO.class, adminUser);
        return materialMapper.websitVenderPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
    }

    public WebsitVender detail(String id) {
        WebsitVender websitVender = websitVenderService.getById(id);
        return websitVender;
    }

    @Transactional(rollbackFor = Exception.class)
    public void add(WebsitVender bean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        bean.setVenderId(null);
        bean.setVenderType("辅材");

        if (StringUtils.isBlank(bean.getIsAuto())) {
            throw new RemoteServiceException("请选择是否自动审核");
        }

        if (Objects.nonNull(adminUser.getAdminCompanyWechat())) {
            bean.setCompanyWechatId(adminUser.getAdminCompanyWechat().getCompanyWechatId());
            bean.setCompanyWechatName(adminUser.getAdminCompanyWechat().getCompanyName());
        }

        WebsitVender vender = new WebsitVender();
        BeanUtils.copyProperties(bean, vender);

        vender.insert();
    }

    @Transactional(rollbackFor = Exception.class)
    public void edit(WebsitVender bean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        bean.setCompanyWechatId(adminUser.getCompanyWechatId());
        bean.setVenderType("辅材");

        if (StringUtils.isBlank(bean.getIsAuto())) {
            throw new RemoteServiceException("请选择是否自动审核");
        }

        WebsitVender oldVender = websitVenderService.getById(bean.getVenderId());
        bean.setCompanyWechatId(oldVender.getCompanyWechatId());
        bean.setCompanyWechatName(oldVender.getCompanyWechatName());

        WebsitVender vender = new WebsitVender();
        BeanUtils.copyProperties(bean, vender);

        vender.updateById();
    }
}
