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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.utils.StringUtil;
import com.zksy.system.basicData.domain.CrmFile;
import com.zksy.system.basicData.domain.OwSelfReporting;
import com.zksy.system.basicData.domain.dto.OwSelfReportingDTO;
import com.zksy.system.basicData.mapper.OwSelfReportingMapper;
import com.zksy.system.basicData.service.CaptchaService;
import com.zksy.system.basicData.service.CrmFileService;
import com.zksy.system.basicData.service.OwSelfReportingService;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * 自主上报Service业务层处理
 *
 * @author sy
 * @date 2024-05-10
 */
@Service
@Slf4j
public class OwSelfReportingServiceImpl extends ServiceImpl<OwSelfReportingMapper, OwSelfReporting> implements OwSelfReportingService {

    @Resource
    private OwSelfReportingMapper mapper;

    @Autowired
    private CrmFileService crmFileService;

    @Autowired
    private CaptchaService captchaService;

    @Override
    public AjaxResult saveOwSelfReporting(OwSelfReportingDTO dto) {
        String msg = captchaService.checkImageCode(dto.getImageKey(), dto.getImageCode());
        if (com.zksy.common.utils.StringUtils.isNotBlank(msg)) {
            return AjaxResult.error(999,msg);
        }
        OwSelfReporting entity = new OwSelfReporting();
        BeanUtils.copyProperties(dto,entity);
        entity.setCreateTime(new Date());
        int number = mapper.insert(entity);
        if(number > 0){
            if(dto.getFiles() != null) {
                for (CrmFile file : dto.getFiles()) {
                    //添加文件
                    file.setFid(entity.getId());
                    file.setUpdateBy(entity.getEnterpriseName());
                    file.setUpdateTime(new Date());
                    crmFileService.updateById(file);
                }
            }
        }else{
            return AjaxResult.success("上报失败");
        }
        return AjaxResult.success("上报成功");
    }

    @Override
    public AjaxResult updateOwSelfReporting(OwSelfReporting owSelfReporting) {
        OwSelfReporting info = mapper.selectById(owSelfReporting.getId());
        log.info("数据长度之前:{}",owSelfReporting.getFiles().size());
        //赋值
        BeanUtils.copyProperties(owSelfReporting, info);
        info.setFiles(null);
        info.setUpdateTime(new Date());
        int number = mapper.updateById(info);
        if(number > 0){
            if(owSelfReporting.getFiles() != null) {
                for (CrmFile file : owSelfReporting.getFiles()) {
                    //添加文件
                    file.setFid(info.getId());
                    file.setUpdateBy(info.getEnterpriseName());
                    file.setUpdateTime(new Date());
                    crmFileService.updateById(file);
                }
            }
        }else{
            return AjaxResult.success("上报失败");
        }
        return AjaxResult.success("上报成功");
    }

    @Override
    public AjaxResult getByOwSelfReportingId(String id) {
        OwSelfReporting owSelfReporting = baseMapper.selectById(id);
        List<CrmFile> crmFile = crmFileService.selectByFid(owSelfReporting.getId());
        if(owSelfReporting != null) {
            owSelfReporting.setFiles(crmFile);
        }
        return AjaxResult.success("查询成功",owSelfReporting);
    }

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

    @Override
    public AjaxResult checkInformationByUnicode(String unicode) {
        if(StringUtil.isBlank(unicode)){
            return AjaxResult.error("企业代码不能为空");
        }
        LambdaQueryWrapper<OwSelfReporting> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OwSelfReporting::getUnifiedSocialCreditCode,unicode);
        List<OwSelfReporting> selfReportingList = mapper.selectList(wrapper);
        selfReportingList.stream().forEach(q -> {
            List<CrmFile> files = crmFileService.selectByFid(q.getId());
            q.setFiles(files);
            q.setContacts(q.getContacts().charAt(0)+"****");
        });
        return AjaxResult.success("查询成功",selfReportingList);
    }
}
