package com.yunzhi.marketing.xlk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.center.taUser.entity.TaUser;
import com.yunzhi.marketing.common.StringUtils;
import com.yunzhi.marketing.xlk.entity.Institution;
import com.yunzhi.marketing.xlk.mapper.InstitutionMapper;
import com.yunzhi.marketing.xlk.service.IInstitutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 组织结构表  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-06-22
 */
@Service
public class InstitutionServiceImpl extends ServiceImpl<InstitutionMapper, Institution> implements IInstitutionService {

    @Autowired
    private InstitutionMapper institutionMapper;
    /**
     * 添加组织机构
     *
     * @param institution
     * @return
     */
    @Override
    public ResponseBean saveInstitution(Institution institution) {

        Institution parentInstitution = institutionMapper.selectById(institution.getParentInstitutionId());
        // 根据code获取机构
        LambdaQueryWrapper<Institution> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Institution::getParentInstitutionId,institution.getParentInstitutionId());
        queryWrapper.orderByDesc(Institution::getInstitutionCode);
        List<Institution> institutions = institutionMapper.selectList(queryWrapper);
        if (institutions.size() > 0) {
            Institution lastInstitution = institutions.get(0);
            String institutionCode = lastInstitution.getInstitutionCode();
            String firstCode = institutionCode.substring(0, institutionCode.length() - 2);
            String lastCode = institutionCode.substring(institutionCode.length() - 2);
            String currentCode =String.valueOf(Integer.parseInt(lastCode) + 1);
            if (currentCode.length() == 1) {
                institution.setInstitutionCode(firstCode + "0" +currentCode);
            }else {
                institution.setInstitutionCode(firstCode + currentCode);
            }

        }else {
            institution.setInstitutionCode(parentInstitution.getInstitutionCode() + "01");
        }

        institution.setCreateDate(LocalDateTime.now());
        institutionMapper.insert(institution);

        return ResponseBean.success(institution);
    }

    /**
     * 删除组织机构
     *
     * @param id
     * @return
     */
    @Override
    public ResponseBean deleteByinstitutionCode(String id) {
        Institution institution = institutionMapper.selectById(id);
        LambdaQueryWrapper<Institution> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.likeLeft(Institution::getInstitutionCode,institution.getInstitutionCode());
        List<Institution> institutions = institutionMapper.selectList(queryWrapper);
        if (institutions.size() > 1) {
            return ResponseBean.error("请先删除子公司", ResponseBean.ERROR_UNAVAILABLE);
        }
        institutionMapper.deleteById(id);
        return ResponseBean.success("删除成功");
    }

    /**
     * 修改组织机构
     *
     * @param institution
     * @return
     */
    @Override
    public ResponseBean updateInstitution(Institution institution) {
        institutionMapper.updateById(institution);
        return ResponseBean.success(institution);
    }

    @Override
    public List<Institution> getAllByUser(TaUser taUser) {
        return institutionMapper.getAllByUser(taUser.getUserId());
    }

    private Institution getChildInstitution(String institutionCode, int start, int end) {
        String blocCode = institutionCode.substring(start, end);
        LambdaQueryWrapper<Institution> blocQuery = new LambdaQueryWrapper<>();
        blocQuery.eq(Institution::getInstitutionCode,blocCode);
        return institutionMapper.selectOne(blocQuery);
    }
}
