package com.zcloud.modules.sys.service.impl;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.zcloud.common.utils.PageUtils;
 import com.zcloud.common.utils.Tools;
 import com.zcloud.modules.custormerInfo.entity.BusCustomerInfoEntity;
 import com.zcloud.modules.custormerInfo.service.BusCustomerInfoService;
 import com.zcloud.modules.sys.dao.BusNoticeDao;
 import com.zcloud.modules.sys.entity.BusNoticeCorpEntity;
 import com.zcloud.modules.sys.entity.BusNoticeEntity;
 import com.zcloud.modules.sys.service.BusNoticeCorpService;
 import com.zcloud.modules.sys.service.BusNoticeService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;

 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;

/**
 * @Author chenxinying
 * @Desc (BusNotice)表服务实现类
 * @Date 2024-03-28 16:29:57
 */
@Service("busNoticeService")
public class BusNoticeServiceImpl extends ServiceImpl<BusNoticeDao, BusNoticeEntity> implements BusNoticeService {
    @Autowired
    private BusCustomerInfoService busCustomerInfoService;
    @Autowired
    private BusNoticeCorpService busNoticeCorpService;

    @Override
    @Transactional
    public void saveNotice(BusNoticeEntity param){
        baseMapper.insert(param);

        if("yes".equals(param.getIsAll())){
            List<BusNoticeCorpEntity> noticeCorps = new ArrayList<>();
            QueryWrapper<BusCustomerInfoEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("is_delete", 0);
            List<BusCustomerInfoEntity> customerList = busCustomerInfoService.list(queryWrapper);
            customerList.stream().forEach(customer -> {
                BusNoticeCorpEntity entity = new BusNoticeCorpEntity();
                entity.setNoticeCorpId(Tools.get32UUID());
                entity.setIsDelete(0);
                entity.setNoticeId(param.getNoticeId());
                entity.setCustomerinfoId(customer.getCustomerinfoId());
                entity.setStatus("0");
                noticeCorps.add(entity);
            });
            busNoticeCorpService.saveBatch(noticeCorps);
        }else{
            List<BusNoticeCorpEntity> noticeCorps = new ArrayList<>();
                String[] customerinfoIds = param.getCustomerinfoId().split(",");
                for (int i = 0; i < customerinfoIds.length; i++) {
                    BusNoticeCorpEntity entitys = new BusNoticeCorpEntity();
                    entitys.setNoticeCorpId(Tools.get32UUID());
                    entitys.setIsDelete(0);
                    entitys.setNoticeId(param.getNoticeId());
                    entitys.setCustomerinfoId(customerinfoIds[i]);
                    entitys.setStatus("0");
                    noticeCorps.add(entitys);
                }

            busNoticeCorpService.saveBatch(noticeCorps);
        }


    }

    @Override
	public BusNoticeEntity queryByFlexibleParams(String paramName, Object paramValue) {
		QueryWrapper<BusNoticeEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(paramName, paramValue); // 根据传入的参数名和参数值动态构建查询条件

		return baseMapper.selectOne(queryWrapper);
	}

	@Override
	public List<BusNoticeEntity> queryListByFlexibleParams(String paramName, Object paramValue) {
		QueryWrapper<BusNoticeEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(paramName, paramValue); // 根据传入的参数名和参数值动态构建查询条件

		return baseMapper.selectList(queryWrapper);
	}

    @Override
    public PageUtils listPage(Map<String, Object> params) {
        Page<Map<String,Object>> page = new Page<>(Integer.parseInt(params.get("curPage").toString()),Integer.parseInt(params.get("limit").toString()));
        params.put("isDelete",0);
        IPage<BusNoticeEntity> ipage = baseMapper.listPage(page,params);
        return new PageUtils(ipage);
    }


   @Override
	public void delete(String noticeId,String userId){
		UpdateWrapper<BusNoticeEntity> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("notice_id",noticeId);

		BusNoticeEntity busNotice = new BusNoticeEntity();
		busNotice.setIsDelete(1);

		this.update(busNotice, updateWrapper);
		UpdateWrapper<BusNoticeCorpEntity> updateWrapper1 = new UpdateWrapper<>();
		updateWrapper1.eq("notice_id",noticeId);
		BusNoticeCorpEntity busNoticeCorp = new BusNoticeCorpEntity();
		busNoticeCorp.setIsDelete(1);
		busNoticeCorpService.update(busNoticeCorp,updateWrapper1);
	}
}
