package com.jeeplus.test.workClientInfo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.utils.DictUtils;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.oss.domain.WorkAttachment;
import com.jeeplus.test.oss.mapper.OssServiceMapper;
import com.jeeplus.test.oss.service.OssService;
import com.jeeplus.test.workClientInfo.domain.WorkClientBank;
import com.jeeplus.test.workClientInfo.domain.WorkClientInfo;
import com.jeeplus.test.workClientInfo.domain.WorkClientJobTypeInfo;
import com.jeeplus.test.workClientInfo.domain.WorkClientLinkman;
import com.jeeplus.test.workClientInfo.domain.dto.PageInfoDto;
import com.jeeplus.test.workClientInfo.domain.dto.WorkClientInfosDto;
import com.jeeplus.test.workClientInfo.mapper.WorkClientBankMapper;
import com.jeeplus.test.workClientInfo.mapper.WorkClientInfoMapper;
import com.jeeplus.test.workClientInfo.mapper.WorkClientJobTypeInfoMapper;
import com.jeeplus.test.workClientInfo.mapper.WorkClientLinkmanMapper;
import lombok.extern.slf4j.Slf4j;
import org.flowable.editor.language.json.converter.util.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Slf4j
@Service
public class WorkClientService {

    @Resource
    private WorkClientInfoMapper workClientInfoMapper;

    @Resource
    private WorkClientBankMapper workClientBankMapper;

    @Resource
    private WorkClientLinkmanMapper workClientLinkmanMapper;

    @Resource
    private OssServiceMapper ossServiceMapper;

    @Resource
    private WorkClientJobTypeInfoMapper workClientJobTypeInfoMapper;

    @Resource
    private OssService ossService;

    /**
     * 客户信息列表
     * @param
     * @return
     */
    public IPage<WorkClientInfo> list(Page<WorkClientInfo> page, QueryWrapper<WorkClientInfo> queryWrapper) {
        queryWrapper.eq("a.del_flag","0");
        IPage<WorkClientInfo> pageList = workClientInfoMapper.findPageList(page,queryWrapper);
        return pageList;
    }

    /**
     * 客户信息保存
     * @param workClientInfosDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String save(WorkClientInfosDto workClientInfosDto) {
        //获取当前登录人信息
        UserDTO dto = UserUtils.getCurrentUserDTO();
        //客户信息处理
        String id = UUID.randomUUID().toString().replace("-", "");
        WorkClientInfo workClientInfo = workClientInfosDto.getWorkClientInfo();
        workClientInfo.setId(id);
        workClientInfo.setCreateBy(dto.getId());
        workClientInfo.setCreateDate(new Date());
        workClientInfo.setUpdateBy(dto.getId());
        workClientInfo.setUpdateDate(new Date());
        workClientInfo.setCompanyId(dto.getCompanyDTO().getId());
        workClientInfo.setOfficeId(dto.getOfficeDTO().getId());
        workClientInfoMapper.insert(workClientInfo);
        //客户行业处理
        String deputy = workClientInfo.getDeputy();
        if (StringUtils.isNotEmpty(deputy)) {
            List<String> list = Arrays.asList(deputy.split(","));
            list.stream().forEach(i -> {
                WorkClientJobTypeInfo info = new WorkClientJobTypeInfo();
                info.setWorkClientId(id);
                info.setJobTypeId(i);
                workClientJobTypeInfoMapper.insert(info);
            });
        }
        //开户行信息处理
        List<WorkClientBank> workClientBank = workClientInfosDto.getWorkClientBank();
        if (CollectionUtils.isNotEmpty(workClientBank)) {
            workClientBank.stream().forEach(i -> {
                i.setId(UUID.randomUUID().toString().replace("-", ""));
                i.setCreateBy(dto.getId());
                i.setCreateDate(new Date());
                i.setUpdateBy(dto.getId());
                i.setUpdateDate(new Date());
                i.setClientId(id);
                workClientBankMapper.insert(i);
            });
        }
        //联系人信息处理
        List<WorkClientLinkman> workClientLinkman = workClientInfosDto.getWorkClientLinkman();
        if (CollectionUtils.isNotEmpty(workClientLinkman)) {
            workClientLinkman.stream().forEach(i -> {
                i.setId(UUID.randomUUID().toString().replace("-", ""));
                i.setCreateBy(dto.getId());
                i.setCreateDate(new Date());
                i.setUpdateBy(dto.getId());
                i.setUpdateDate(new Date());
                i.setClientId(id);
                workClientLinkmanMapper.insert(i);
            });
        }
        //附件信息处理
        List<WorkAttachment> workAttachments = workClientInfosDto.getWorkAttachments();
        if (CollectionUtils.isNotEmpty(workAttachments)) {
            int j = 1;
            for (WorkAttachment i : workAttachments) {
                i.setId(UUID.randomUUID().toString().replace("-", ""));
                i.setCreateBy(dto.getId());
                i.setCreateDate(new Date());
                i.setUpdateBy(dto.getId());
                i.setUpdateDate(new Date());
                i.setAttachmentId(id);
                i.setSort(j);
                ossServiceMapper.insert(i);
                j++;
            }
        }
        return "添加完成!";
    }

    /**
     * 客户信息删除
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String removeById (String id) {
        //客户信息删除
        workClientInfoMapper.deleteById(id);
        //客户行业处理
        LambdaQueryWrapper<WorkClientJobTypeInfo> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(WorkClientJobTypeInfo::getWorkClientId, id);
        workClientJobTypeInfoMapper.delete(wrapper3);
        //客户关联开户行信息删除
        LambdaQueryWrapper<WorkClientBank> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WorkClientBank::getClientId, id);
        workClientBankMapper.delete(wrapper);
        //客户关联紧急联系人信息删除
        LambdaQueryWrapper<WorkClientLinkman> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(WorkClientLinkman::getClientId, id);
        workClientLinkmanMapper.delete(wrapper1);
        //客户关联附件信息删除
        LambdaQueryWrapper<WorkAttachment> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(WorkAttachment::getAttachmentId, id);
        ossServiceMapper.delete(wrapper2);
        return "删除完成!";
    }

    /**
     * 客户信息查询
     * @param id
     * @return
     */
    public WorkClientInfosDto findById (String id) {
        WorkClientInfosDto dto = new WorkClientInfosDto();
        WorkClientInfo workClientInfo = workClientInfoMapper.selectById(id);
        //客户行业查询
        LambdaQueryWrapper<WorkClientJobTypeInfo> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(WorkClientJobTypeInfo::getWorkClientId, id);
        List<WorkClientJobTypeInfo> infos = workClientJobTypeInfoMapper.selectList(wrapper3);
        if (CollectionUtils.isNotEmpty(infos)) {
            String s = new String();
            for (WorkClientJobTypeInfo info : infos) {
                String jobTypeId = info.getJobTypeId();
                s = s + "," + jobTypeId;
            }
            String substring = s.substring(1, s.length());
            workClientInfo.setDeputy(substring);
        }
        //客户信息查询
        dto.setWorkClientInfo(workClientInfo);
        //客户关联开户行信息查询
        LambdaQueryWrapper<WorkClientBank> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WorkClientBank::getClientId, id);
        wrapper.orderByDesc(WorkClientBank::getUpdateDate);
        dto.setWorkClientBank(workClientBankMapper.selectList(wrapper));
        //客户关联紧急联系人信息查询
        LambdaQueryWrapper<WorkClientLinkman> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(WorkClientLinkman::getClientId, id);
        wrapper1.orderByDesc(WorkClientLinkman::getUpdateDate);
        dto.setWorkClientLinkman(workClientLinkmanMapper.selectList(wrapper1));
        //客户关联附件信息查询
        LambdaQueryWrapper<WorkAttachment> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(WorkAttachment::getAttachmentId, id);
        wrapper2.orderByAsc(WorkAttachment::getSort);
        dto.setWorkAttachments(ossServiceMapper.selectList(wrapper2));
        return dto;
    }

    /**
     * 客户信息更新
     * @param workClientInfosDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String update(WorkClientInfosDto workClientInfosDto) {
        //获取当前登录人信息
        UserDTO dto = UserUtils.getCurrentUserDTO();
        //客户信息处理
        WorkClientInfo workClientInfo = workClientInfosDto.getWorkClientInfo();
        //客户行业处理
        LambdaQueryWrapper<WorkClientJobTypeInfo> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(WorkClientJobTypeInfo::getWorkClientId, workClientInfo.getId());
        workClientJobTypeInfoMapper.delete(wrapper3);
        String deputy = workClientInfo.getDeputy();
        if (StringUtils.isNotEmpty(deputy)) {
            List<String> list = Arrays.asList(deputy.split(","));
            list.stream().forEach(i -> {
                WorkClientJobTypeInfo info = new WorkClientJobTypeInfo();
                info.setWorkClientId(workClientInfo.getId());
                info.setJobTypeId(i);
                workClientJobTypeInfoMapper.insert(info);
            });
        }
        workClientInfo.setUpdateBy(dto.getId());
        workClientInfo.setUpdateDate(new Date());
        workClientInfo.setCompanyId(dto.getCompanyDTO().getId());
        workClientInfo.setOfficeId(dto.getOfficeDTO().getId());
        workClientInfoMapper.updateById(workClientInfo);
        //开户行信息处理(先删除原有数据，再进行数据添加)
        List<WorkClientBank> workClientBank = workClientInfosDto.getWorkClientBank();
        if (CollectionUtils.isNotEmpty(workClientBank)) {
            LambdaQueryWrapper<WorkClientBank> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(WorkClientBank::getClientId, workClientInfo.getId());
            workClientBankMapper.delete(wrapper);
            workClientBank.stream().forEach(i -> {
                i.setId(UUID.randomUUID().toString().replace("-",""));
                i.setCreateBy(dto.getId());
                i.setCreateDate(new Date());
                i.setUpdateBy(dto.getId());
                i.setUpdateDate(new Date());
                i.setClientId(workClientInfo.getId());
                workClientBankMapper.insert(i);
            });
        }
        //联系人信息处理
        List<WorkClientLinkman> workClientLinkman = workClientInfosDto.getWorkClientLinkman();
        if (CollectionUtils.isNotEmpty(workClientLinkman)) {
            LambdaQueryWrapper<WorkClientLinkman> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(WorkClientLinkman::getClientId, workClientInfo.getId());
            workClientLinkmanMapper.delete(wrapper);
            workClientLinkman.stream().forEach(i -> {
                i.setId(UUID.randomUUID().toString().replace("-",""));
                i.setCreateBy(dto.getId());
                i.setCreateDate(new Date());
                i.setUpdateBy(dto.getId());
                i.setUpdateDate(new Date());
                i.setClientId(workClientInfo.getId());
                workClientLinkmanMapper.insert(i);
            });
        }
        //附件信息处理
        List<WorkAttachment> workAttachments = workClientInfosDto.getWorkAttachments();
        if (CollectionUtils.isNotEmpty(workAttachments)) {
            workAttachments.stream().forEach(i -> i.setAttachmentId(workClientInfo.getId()));
            ossService.saveMsg(workAttachments);
        }
        return "更新完成!";
    }

    /**
     * 根据名称模糊搜索客户信息
     * @param name
     * @return
     */
    public List<WorkClientInfo> findListByName(String name) {
        LambdaQueryWrapper<WorkClientInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(WorkClientInfo::getName, name);
        List<WorkClientInfo> workClientInfos = workClientInfoMapper.selectList(wrapper);
        return workClientInfos;
    }
}
