package com.zfire.mall.manager.logic.record;

import com.aliyuncs.utils.StringUtils;
import com.zfire.mall.manager.bean.record.ImgCarouselManageBean;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.plus.entity.CompanyRecordManage;
import com.zfire.mall.manager.plus.entity.ImgCarouselManage;
import com.zfire.mall.manager.plus.service.CompanyRecordManageService;
import com.zfire.mall.manager.plus.service.ImgCarouselManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CompanyRecordLogic {

    @Autowired
    CompanyRecordManageService companyRecordManageService;

    @Autowired
    ImgCarouselManageService imgCarouselManageService;

    /**
     * 插入公司备案信息
     */
    @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;
    }

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


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

    /**
     * 保存轮播图信息
     */
    @Transactional
    public void addImgCarousel(ImgCarouselManage imgCarouselManage) throws RemoteServiceException{
        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();
    }



}
