package com.hzs.system.home.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.system.base.BdArea;
import com.hzs.common.domain.system.ext.HoGlobalCompanyExt;
import com.hzs.common.domain.system.home.HoGlobalCompany;
import com.hzs.system.base.service.IBdAreaService;
import com.hzs.system.home.mapper.HoGlobalCompanyMapper;
import com.hzs.system.home.service.IHoGlobalCompanyService;
import com.hzs.system.home.vo.HoGlobalCompanyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * <p>
 * 全球分布 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2022-12-08
 */
@Service
public class HoGlobalCompanyServiceImpl extends ServiceImpl<HoGlobalCompanyMapper, HoGlobalCompany> implements IHoGlobalCompanyService {

    @Autowired
    private IBdAreaService iBdAreaService;
    /**
     * @description: 查询全球分布列表
     * @author: zhang jing
     * @date: 2023/2/28 17:51
     * @param: [hoGlobalCompanyExt]
     * @return: java.util.List<com.hzs.common.domain.system.ext.HoGlobalCompanyExt>
     **/
    @Override
    public List<HoGlobalCompanyVO> queryGlobalCompanyList(HoGlobalCompanyVO globalCompanyVO) {
        if(globalCompanyVO.getEndDate() != null){
            globalCompanyVO.setEndDate(DateUtils.afterDate(1, ChronoUnit.DAYS, globalCompanyVO.getEndDate()));
        }
        return baseMapper.queryGlobalCompanyList(globalCompanyVO);
    }

    /**
     * @description: 按区域查询
     * @author: zhang jing
     * @date: 2023/4/6 9:39
     * @param: [hoGlobalCompanyExt]
     * @return: java.util.List<com.hzs.common.domain.system.ext.HoGlobalCompanyExt>
     **/
    @Override
    public List<HoGlobalCompanyVO> queryGlobalByContinent(HoGlobalCompanyVO globalCompanyVO) {
        return baseMapper.queryGlobalByContinent(globalCompanyVO);
    }


    /**
     * @description: 新增全球分布
     * @author: zhang jing
     * @date: 2023/6/27 11:12
     * @param: [hoGlobalCompany]
     * @return: boolean
     **/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveCompany(HoGlobalCompany hoGlobalCompany) {
        baseMapper.insert(hoGlobalCompany);
        UpdateWrapper<BdArea> updateWrapper=new UpdateWrapper();
        updateWrapper.eq("PK_ID",hoGlobalCompany.getGloProvince());
        updateWrapper.set("IS_ILLUMINATE",0);
        return iBdAreaService.update(updateWrapper);
    }

    /**
     * @description:修改全球分布
     * @author: zhang jing
     * @date: 2023/6/27 11:28
     * @param: [hoGlobalCompany]
     * @return: boolean
     **/
    @Override
    public boolean updateCompany(HoGlobalCompany hoGlobalCompany) {
        HoGlobalCompany old=  baseMapper.selectById(hoGlobalCompany.getPkId());
        QueryWrapper<HoGlobalCompany> queryWrapper=new QueryWrapper();
        queryWrapper.eq("GLO_PROVINCE",old.getGloProvince());
       List<HoGlobalCompany> list= baseMapper.selectList(queryWrapper);

        UpdateWrapper<BdArea> updateWrapper=new UpdateWrapper();
       if(CollectionUtil.isEmpty(list)){
           updateWrapper.eq("PK_ID",old.getGloProvince());
           updateWrapper.set("IS_ILLUMINATE",1);
           iBdAreaService.update(updateWrapper);
       }
        updateWrapper.eq("PK_ID",hoGlobalCompany.getGloProvince());
        updateWrapper.set("IS_ILLUMINATE",0);
        iBdAreaService.update(updateWrapper);

        baseMapper.updateById(hoGlobalCompany);
        return true;
    }
}
