package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysSymbolDao;
import com.crm.manager.dao.repository.SysSymbolRepository;
import com.crm.manager.dao.repository.SysSymbolScaleRepository;
import com.crm.manager.dao.repository.SysSymbolTypeRepository;
import com.crm.manager.service.SysSymbolService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.system.symbol.SysSymbolDto;
import com.crm.rely.backend.core.dto.system.symbol.SysSymbolScaleDto;
import com.crm.rely.backend.core.dto.system.symbol.SysSymbolStaticDto;
import com.crm.rely.backend.core.dto.system.symbol.SysSymbolTypeDto;
import com.crm.rely.backend.core.entity.system.symbol.SysSymbolAddEntity;
import com.crm.rely.backend.core.entity.system.symbol.SysSymbolListEntity;
import com.crm.rely.backend.core.entity.system.symbol.SysSymbolUpdateEntity;
import com.crm.rely.backend.core.pojo.table.SysSymbolScaleTable;
import com.crm.rely.backend.core.pojo.table.SysSymbolTable;
import com.crm.rely.backend.core.pojo.table.SysSymbolTypeTable;
import com.crm.rely.backend.core.pojo.view.SysSymbolView;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.FormatPage;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by max on 2020/7/8.
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Service
public class SysSymbolServiceImpl implements SysSymbolService {

    @Autowired
    private SysSymbolRepository sysSymbolRepository;
    @Autowired
    private SysSymbolDao sysSymbolDao;
    @Autowired
    private SysSymbolTypeRepository sysSymbolTypeRepository;
    @Autowired
    private SysSymbolScaleRepository sysSymbolScaleRepository;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysSymbolAddEntity entity) throws Exception {
        /*
            code是否存在
         */
        Integer unique = sysSymbolRepository.countByCode(entity.getCode());
        if (unique >= 1) {
            throw new ServiceException("code_exists");
        }
        /*
            保存信息
         */
        SysSymbolTable table = new SysSymbolTable();
        BeanUtils.copyProperties(entity, table);
        sysSymbolRepository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(SysSymbolUpdateEntity entity) throws Exception {
        /*
            获取原信息
         */
        SysSymbolTable table = sysSymbolRepository.getFirstById(entity.getId());
        if (table == null) {
            throw new ServiceException("record_not_found");
        }
        /*
            判断code是否存在
         */
        if (!entity.getCode().equals(table.getCode())) {
            Integer unique = sysSymbolRepository.countByCode(entity.getCode());
            if (unique >= 1) {
                throw new ServiceException("code_exists");
            }
        }
        /*
            更新信息
         */
        BeanUtils.copyProperties(entity, table);
        sysSymbolRepository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long id) throws Exception {
        /*
            获取原信息
         */
        SysSymbolTable table = sysSymbolRepository.getFirstById(id);
        if (table == null) {
            throw new ServiceException("record_not_found");
        }
        /*
            删除信息
         */
        sysSymbolRepository.delete(id);
    }

    @Override
    public SysSymbolDto getById(Long id) throws Exception {

        SysSymbolView view = sysSymbolDao.getOne(id);
        if (view == null) {
            throw new ServiceException("record_not_found");
        }

        SysSymbolDto sysSymbolDto = copyFromView(view);
        return sysSymbolDto;
    }

    @Override
    public BaseResultWithPagerDto<List<SysSymbolDto>> getList(SysSymbolListEntity entity) throws Exception {

        //获取总数
        Integer count = sysSymbolDao.countList(entity);
        PageDto pageDto = FormatPage.format(entity.getPage(), count);
        if (count == null || count == 0) {
            return new BaseResultWithPagerDto(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, pageDto);
        }
        /*
            获取列表
         */
        List<SysSymbolView> views = sysSymbolDao.getList(entity);
        List<SysSymbolDto> dtos = Lists.newArrayList();

        views.forEach((view)->{
            SysSymbolDto dto = copyFromView(view);
            dtos.add(dto);
        });

        return new BaseResultWithPagerDto(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, pageDto, dtos);
    }

    @Override
    public SysSymbolStaticDto getSymbolStaticData() throws Exception {
        /*
            品种类型
         */
        List<SysSymbolTypeTable> typeTables = sysSymbolTypeRepository.findAll();
        List<SysSymbolTypeDto> typeDtos = Lists.newArrayList();
        typeTables.forEach((typeTable) ->{
            SysSymbolTypeDto typeDto = new SysSymbolTypeDto();
            BeanUtils.copyProperties(typeTable, typeDto);
            typeDtos.add(typeDto);
        });


        /*
            合约大小
         */
        List<SysSymbolScaleTable> scaleTables = sysSymbolScaleRepository.findAll();
        List<SysSymbolScaleDto> scaleDtos = Lists.newArrayList();
        scaleTables.forEach((scaleTable)->{
            SysSymbolScaleDto scaleDto = new SysSymbolScaleDto();
            BeanUtils.copyProperties(scaleTable,scaleDto);
            scaleDto.setId(scaleTable.getScale());
            scaleDtos.add(scaleDto);
        });

        SysSymbolStaticDto dto = new SysSymbolStaticDto();
        dto.setScales(scaleDtos);
        dto.setTypes(typeDtos);
        return dto;
    }

    private SysSymbolDto copyFromView(SysSymbolView view){
        SysSymbolDto sysSymbolDto = new SysSymbolDto();
        BeanUtils.copyProperties(view, sysSymbolDto);
        return sysSymbolDto;
    }
}
