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


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.common.AmityUrlVO;
import com.gree.mall.manager.bean.common.ImgCarouselManageBean;
import com.gree.mall.manager.commonmapper.CommonMapper;
import com.gree.mall.manager.enums.IsYesNoEnum;
import com.gree.mall.manager.enums.StatusEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.plus.entity.AmityUrl;
import com.gree.mall.manager.plus.entity.CompanyRecordManage;
import com.gree.mall.manager.plus.entity.ImgCarouselManage;
import com.gree.mall.manager.plus.service.AmityUrlService;
import com.gree.mall.manager.plus.service.CompanyRecordManageService;
import com.gree.mall.manager.plus.service.ImgCarouselManageService;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class CompanyRecordLogic {

    @Autowired
    ImgCarouselManageService imgCarouselManageService;

    @Autowired
    CompanyRecordManageService companyRecordManageService;

    @Autowired
    AmityUrlService amityUrlService;

    @Autowired
    CommonMapper commonMapper;


    /**
     * 插入公司备案信息
     */
    @Transactional
    public void addRecordCompany(CompanyRecordManage companyRecordManage){
        if(StringUtils.isEmpty(companyRecordManage.getId())){
            companyRecordManageService.save(companyRecordManage);
        } else {
            companyRecordManage.updateById();
        }
    }

    @Transactional
    public CompanyRecordManage  getCompany(String id){
        CompanyRecordManage companyRecordManage = companyRecordManageService.getById(id);
        return companyRecordManage;
    }

    @Transactional
    public void editRecordCompany(CompanyRecordManage companyRecordManage){
        companyRecordManage.updateById();
    }

    /**
     * 展示轮播图列表信息
     */
    public List<CompanyRecordManage> companyRecordList(){
        List<CompanyRecordManage> list = companyRecordManageService.lambdaQuery().list();
        return list;
    }


    /**
     * 保存轮播图信息
     */
    @Transactional
    public void addImgCarousel(ImgCarouselManage imgCarouselManage) {
        if(StringUtils.isEmpty(imgCarouselManage.getStatus())){
            throw new RemoteServiceException("状态不能为空");
        }
        imgCarouselManageService.save(imgCarouselManage);
    }

    /**
     * 展示轮播图列表信息
     */
    public List<ImgCarouselManage> list(){
        List<ImgCarouselManage> list = imgCarouselManageService.lambdaQuery().list();
        return list;
    }

    /**
     * 单个 批量 删除轮播图信息
     */
    public void delete(List<String> ids){
        imgCarouselManageService.lambdaUpdate().in(ImgCarouselManage::getId,ids).remove();
        //imgCarouselManageService.removeById(id);
    }

    public ImgCarouselManage detail(String id){
        ImgCarouselManage imgCarouselManage = imgCarouselManageService.getById(id);
        return imgCarouselManage;
    }

    @Transactional
    public void update(ImgCarouselManageBean imgCarouselManageBean){
        ImgCarouselManage imgCarouselManage = imgCarouselManageService.getById(imgCarouselManageBean.getId());
        if(StringUtils.isNotEmpty(imgCarouselManageBean.getImgCarouselName())){
            imgCarouselManage.setImgCarouselName(imgCarouselManageBean.getImgCarouselName());
        }
        if(StringUtils.isNotEmpty(imgCarouselManageBean.getImgCarouselUrl())){
            imgCarouselManage.setImgCarouselUrl(imgCarouselManageBean.getImgCarouselUrl());
        }
        if(StringUtils.isNotEmpty(imgCarouselManageBean.getStatus())){
            imgCarouselManage.setStatus(imgCarouselManageBean.getStatus());
        }
        if(imgCarouselManageBean.getImgCarouselOrder() != null){
            imgCarouselManage.setImgCarouselOrder(imgCarouselManageBean.getImgCarouselOrder());
        }
        imgCarouselManage.updateById();
    }


    @Transactional
    public void updateStatus(String id){
        ImgCarouselManage imgCarouselManage = imgCarouselManageService.getById(id);
        if(ImgCarouselManageBean.STATUS_SHOW.equals(imgCarouselManage.getStatus())){
            imgCarouselManage.setStatus(ImgCarouselManageBean.STATUS_HIDE);
        } else {
            imgCarouselManage.setStatus(ImgCarouselManageBean.STATUS_SHOW);
        }
        imgCarouselManage.updateById();
    }

    public void addAmity(AmityUrl amityUrl) {
        if (StringUtils.isEmpty(amityUrl.getAmityUrlName())) {
            throw new RemoteServiceException("友链名称");
        }
        if (StringUtils.isEmpty(amityUrl.getAmityUrl())) {
            throw new RemoteServiceException("友链地址");
        }
        amityUrl.insert();
    }

    public void updateAmity(AmityUrl amityUrl) {
        if (StringUtils.isEmpty(amityUrl.getAmityUrlName())) {
            throw new RemoteServiceException("友链名称");
        }
        if (StringUtils.isEmpty(amityUrl.getAmityUrl())) {
            throw new RemoteServiceException("友链地址");
        }
        amityUrl.updateById();
    }

    public void deleteAmity(List<String> amityUrlIds) {
        amityUrlService.removeByIds(amityUrlIds);
    }

    public IPage<AmityUrlVO> amityList(ZfireParamBean zfireParamBean) {
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        return commonMapper.amityList(new Page(zfireParamBean.getPageNum(),zfireParamBean.getPageSize()),zfireParamBean);
    }

    public List<AmityUrl> list2(){
        return amityUrlService.list();
    }


    public void amityStatus(String amityUrlId, IsYesNoEnum status) {
        AmityUrl amityUrl = new AmityUrl();
        amityUrl.setStatus(status.toString())
                .setAmityUrlId(amityUrlId)
                .updateById();
    }

}
