package com.zksy.system.basicData.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.system.basicData.domain.CrmFile;
import com.zksy.system.basicData.domain.CrmNewsRelease;
import com.zksy.system.basicData.mapper.CrmNewsReleaseMapper;
import com.zksy.system.basicData.service.CrmFileService;
import com.zksy.system.basicData.service.CrmNewsReleaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.zksy.common.utils.SecurityUtils.getUsername;

/**
 * 新闻发布Service业务层处理
 *
 * @author sy
 * @date 2024-05-21
 */
@Service
public class CrmNewsReleaseServiceImpl extends ServiceImpl<CrmNewsReleaseMapper, CrmNewsRelease> implements CrmNewsReleaseService {

    @Resource
    private CrmNewsReleaseMapper mapper;

    @Autowired
    private CrmFileService crmFileService;

    @Override
    public AjaxResult saveCrmNewsRelease(CrmNewsRelease crmNewsRelease) {
        CrmNewsRelease entity = new CrmNewsRelease();
        BeanUtils.copyProperties(crmNewsRelease,entity);
        entity.setCreateBy(entity.getDataSource());
        entity.setCreateTime(new Date());
        int number = mapper.insert(entity);
        if(number > 0){
            if(crmNewsRelease.getFiles() != null) {
                for (CrmFile file : crmNewsRelease.getFiles()) {
                    //添加文件
                    file.setFid(entity.getId());
                    file.setUpdateBy(getUsername());
                    file.setUpdateTime(new Date());
                    crmFileService.updateById(file);
                }
            }
        }else{
            return AjaxResult.success("新增失败");
        }
        return AjaxResult.success("新增成功");
    }

    @Override
    public AjaxResult updateCrmNewsRelease(CrmNewsRelease crmNewsRelease) {
        CrmNewsRelease info = mapper.selectById(crmNewsRelease.getId());
        //赋值
        BeanUtils.copyProperties(crmNewsRelease, info);
        info.setCreateBy(info.getDataSource());
        info.setUpdateTime(new Date());
        int number = mapper.updateById(info);
        if(number > 0 ){
            if(crmNewsRelease.getFiles() != null) {
                for (CrmFile file : crmNewsRelease.getFiles()) {
                    //添加文件
                    file.setFid(info.getId());
                    file.setUpdateBy(getUsername());
                    file.setUpdateTime(new Date());
                    crmFileService.updateById(file);
                }
            }
        }else{
            return AjaxResult.success("修改失败");
        }
        return AjaxResult.success("修改成功");
    }

    @Override
    public AjaxResult getByCrmNewsReleaseId(String id) {
        CrmNewsRelease crmNewsRelease = baseMapper.selectById(id);
        List<CrmFile> crmFile = crmFileService.selectByFid(crmNewsRelease.getId());
        crmNewsRelease.setFiles(crmFile);
        return AjaxResult.success("查询成功",crmNewsRelease);
    }

    @Override
    public AjaxResult removeCrmNewsRelease(List<String> ids) {
        //查询fids所有文件
        List<CrmFile> crmFileList = crmFileService.selectByFids(ids);
        for(CrmFile crmFile : crmFileList){
            //根据id删除文件
            crmFileService.removeById(crmFile.getId());
        }
        mapper.deleteBatchIds(ids);
        return AjaxResult.success("删除文件成功");
    }
}
