package com.sptg.estate.biz.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.estate.api.dto.NoticeDto;
import com.sptg.estate.api.entity.Notice;
import com.sptg.estate.api.entity.NoticeImg;
import com.sptg.estate.biz.mapper.NoticeMapper;
import com.sptg.estate.biz.service.NoticeImgServiceInterface;
import com.sptg.estate.biz.service.NoticeServiceInterface;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * title: NoticeServiceImpl
 * package: com.sptg.estate.biz.security.impl
 * description: 公共-服务层实现层
 *
 *
 *
 */
@Service
@AllArgsConstructor
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeServiceInterface {

    private  final NoticeImgServiceInterface noticeImageServiceInterface;

    @Override
    public Page<Notice> showNoticeList(NoticeDto noticeDto) {
        Page<Notice> page=noticeDto.getPage();
        EntityWrapper<Notice> wrapper=new EntityWrapper<Notice>();
        //标题
        String title=noticeDto.getTitle();
        //编号
        String code=noticeDto.getCode();
        //开始时间条件
        Date startDateCondition=noticeDto.getStartDateCondition();
        //结束时间条件
        Date endDateCondition=noticeDto.getEndDateCondition();
        //机构
        Long orgId=noticeDto.getOrgId();
        //根机构
        Long rootOrgId=noticeDto.getRootOrgId();
        //机构位置
        String orgPosition=noticeDto.getOrgPosition();
        if(StringUtils.isNotBlank(title)) {
            wrapper.like("s_title", title);
        }
        if(StringUtils.isNotBlank(code)){
            wrapper.like("s_code",code);
        }
        if(startDateCondition!=null){
            wrapper.ge("t_create_date",startDateCondition);
        }
        if(endDateCondition!=null){
            wrapper.le("t_create_date",endDateCondition);
        }
        //正常状态
        wrapper.eq("i_status",OperationConstant.ZERO);
        if(orgId!=null){
            wrapper.eq("l_org_id",orgId);
        }
        if(rootOrgId!=null){
            wrapper.eq("l_root_org_id",rootOrgId);
        }
        if(StringUtils.isNotBlank(orgPosition)){
            wrapper.like("s_org_position",orgPosition, SqlLike.RIGHT);
        }
        //排序字段 时间排序
        wrapper.orderBy("t_create_date",false);
        //分页查询
        List<Notice> noticeList=this.baseMapper.showNoticeList(page,wrapper);
        page.setRecords(noticeList);
        return page;
    }
    @Override
    public List<Notice> getList(NoticeDto noticeDto) {
        EntityWrapper<Notice> wrapper=new EntityWrapper<Notice>();
        //标题
        String title=noticeDto.getTitle();
        //编号
        String code=noticeDto.getCode();
        //标题或编号合并标题查询
        boolean isTitleOrNumberSelect=noticeDto.isTitleOrNumberSelect();
        //开始时间条件
        Date startDateCondition=noticeDto.getStartDateCondition();
        //结束时间条件
        Date endDateCondition=noticeDto.getEndDateCondition();
        //机构
        Long orgId=noticeDto.getOrgId();
        //根机构
        Long rootOrgId=noticeDto.getRootOrgId();
        //机构位置
        String orgPosition=noticeDto.getOrgPosition();
        if(isTitleOrNumberSelect&&StringUtils.isNotBlank(title)){
            wrapper.like("s_title", title).or().like("s_code",title);
        }else{
            if(StringUtils.isNotBlank(title)) {
                wrapper.like("s_title", title);
            }
            if(StringUtils.isNotBlank(code)){
                wrapper.like("s_code",code);
            }
        }
        wrapper.andNew();
        if(startDateCondition!=null){
            wrapper.ge("t_create_date",startDateCondition);
        }
        if(endDateCondition!=null){
            wrapper.le("t_create_date",endDateCondition);
        }
        //正常状态
        wrapper.eq("i_status",OperationConstant.ZERO);
        if(orgId!=null){
            wrapper.eq("l_org_id",orgId);
        }
        if(rootOrgId!=null){
            wrapper.eq("l_root_org_id",rootOrgId);
        }
        if(StringUtils.isNotBlank(orgPosition)){
            wrapper.like("s_org_position",orgPosition, SqlLike.RIGHT);
        }
        if(noticeDto.getResidentialId() != null){
            wrapper.andNew().eq("i_type", 2).or().eq("s_residentialIds",noticeDto.getResidentialId().toString());
//            wrapper.like("s_residentialIds",noticeDto.getResidentialId().toString());
        }else{
            wrapper.or().eq("i_type", 2);
        }
        //查询
        return  this.selectList(wrapper);
    }
    @Override
    public boolean addOrEditNotice(Notice notice) {
        Long id=notice.getId();
        //删除图片
        if(id!=null&&id>0){
            noticeImageServiceInterface.deleteNoticeImageByNoticeId(id);
        }else{
            //获取时间对象
            Date date =new Date();
            //获取日期字符串
            String s = new SimpleDateFormat("yyMMdd").format(date);
            Wrapper<Notice> wrapper = new EntityWrapper<>();
            wrapper.like("s_code",s);
            //统计当天新增的公告条数
            int count = this.selectCount(wrapper);
            String n=String.format("%02d",count);
            //公告编号的格式用yyMMdd+2位排序号前面补0
            notice.setCode(s+n);
            //创建时间
            notice.setCreateDate(date);
            //删除标记，0正常
            notice.setStatus(OperationConstant.ZERO);
        }
        //插入更新公告
        boolean result = this.insertOrUpdate(notice);
        //添加图片
        List<NoticeImg> noticeImageList = notice.getNoticeImages();
        if(noticeImageList!=null&&!noticeImageList.isEmpty()){
            //设置关联id值
            noticeImageList.forEach((NoticeImg noticeImage)->{
                noticeImage.setNoticeId(notice.getId());
            });
            noticeImageServiceInterface.insertBatch(noticeImageList);
        }
        return result;
    }
    @Override
    public Notice findNoticeById(long id){
        Notice notice = this.selectById(id);
        EntityWrapper<NoticeImg> wrapper=new EntityWrapper<NoticeImg>();
        wrapper.eq("l_notice_id",id);
        List<NoticeImg> noticeImages = noticeImageServiceInterface.selectList(wrapper);
        notice.setNoticeImages(noticeImages);
        return notice;
    }

    @Override
    public boolean deleteNoticeById(long id){
        Notice notice = this.selectById(id);
        //删除
        notice.setStatus(OperationConstant.MINUS_ONE);
        boolean result=this.updateById(notice);
        return result;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultBean<Boolean> deleteNoticeList(NoticeDto noticeDto){
        List<String> ids = noticeDto.getIds();
        for(String id : ids){
            Notice notice = this.selectById(Long.valueOf(id));
            Assert.notNull(notice,"记录不存在,请核实后重新操作");
            notice.setStatus(OperationConstant.MINUS_ONE);
            this.updateById(notice);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,true);
    }
}
