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.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) {
        int institutionCodeLength = institution.getInstitutionCode().length();
        if (institutionCodeLength != 2 && institutionCodeLength != 4 && institutionCodeLength != 6){
            return ResponseBean.error("请输入合法长度的code", ResponseBean.ERROR_UNAVAILABLE);
        }

        // 根据code获取机构
        LambdaQueryWrapper<Institution> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Institution::getInstitutionCode,institution.getInstitutionCode());
        Institution childInstitution = institutionMapper.selectOne(queryWrapper);
        if (null != childInstitution){
            return ResponseBean.error("此组织机构code已存在", ResponseBean.ERROR_UNAVAILABLE);
        }

        // 判断组织机构长度
        // 集团插入
        if (institutionCodeLength == 2){
            institution.setType(1);
        }

        // 子区域插入
        if (institutionCodeLength == 4){
            Institution blocInstitution = getChildInstitution(institution.getInstitutionCode(), 0, 2);
            if (null == blocInstitution){
                return ResponseBean.error("请输入合法的有层级的code", ResponseBean.ERROR_UNAVAILABLE);
            }
            institution.setType(2);
        }

        // 子区域插入
        if (institutionCodeLength == 6){
            Institution blocInstitution = getChildInstitution(institution.getInstitutionCode(), 0, 2);
            if (null == blocInstitution){
                return ResponseBean.error("请输入合法的有层级的code", ResponseBean.ERROR_UNAVAILABLE);
            }

            Institution cityInstitution = getChildInstitution(institution.getInstitutionCode(), 2, 4);
            if (null == cityInstitution){
                return ResponseBean.error("请输入合法的有层级的code", ResponseBean.ERROR_UNAVAILABLE);
            }
            institution.setType(3);
        }
        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) {
        int institutionCodeLength = institution.getInstitutionCode().length();
        if (institutionCodeLength != 2 && institutionCodeLength != 4 && institutionCodeLength != 6){
            return ResponseBean.error("请输入合法长度的code", ResponseBean.ERROR_UNAVAILABLE);
        }

        // 根据code获取机构
        LambdaQueryWrapper<Institution> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Institution::getInstitutionCode,institution.getInstitutionCode());
        queryWrapper.ne(Institution::getInstitutionCode,institution.getInstitutionCode());
        Institution childInstitution = institutionMapper.selectOne(queryWrapper);
        if (null != childInstitution){
            return ResponseBean.error("此组织机构code已存在", ResponseBean.ERROR_UNAVAILABLE);
        }

        // 判断组织机构长度
        // 集团插入
        if (institutionCodeLength == 2){
            institution.setType(1);
        }

        // 子区域插入
        if (institutionCodeLength == 4){
            Institution blocInstitution = getChildInstitution(institution.getInstitutionCode(), 0, 1);
            if (null == blocInstitution){
                return ResponseBean.error("请输入合法的有层级的code", ResponseBean.ERROR_UNAVAILABLE);
            }
            institution.setType(2);
        }

        // 子区域插入
        if (institutionCodeLength == 6){
            Institution blocInstitution = getChildInstitution(institution.getInstitutionCode(), 0, 1);
            if (null == blocInstitution){
                return ResponseBean.error("请输入合法的有层级的code", ResponseBean.ERROR_UNAVAILABLE);
            }

            Institution cityInstitution = getChildInstitution(institution.getInstitutionCode(), 2, 3);
            if (null == cityInstitution){
                return ResponseBean.error("请输入合法的有层级的code", ResponseBean.ERROR_UNAVAILABLE);
            }
            institution.setType(3);
        }
        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);
    }
}
