package com.sptg.estate.biz.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.estate.api.dto.AgencyDto;
import com.sptg.estate.api.entity.Agency;
import com.sptg.estate.biz.mapper.AgencyMapper;
import com.sptg.estate.biz.service.AgencyServiceInterface;
import com.sptg.permissions.api.dto.OrgDto;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.feigns.OrgFeign;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created By lidexi in 2021/1/8
 * 社区 服务实现
 **/
@Service
@RequiredArgsConstructor
public class AgencyServiceImpl extends ServiceImpl<AgencyMapper, Agency> implements AgencyServiceInterface {

    private final OrgFeign orgFeign;


    @Override
    public ResultBean<Page<Agency>> showAgencyList(AgencyDto agencyDto) {
        Wrapper<Agency> wrapper = new EntityWrapper<>();
        wrapper.eq("i_status",0);
        if(agencyDto.getAgencyName() != null){
            wrapper.like("s_agency_name",agencyDto.getAgencyName(), SqlLike.DEFAULT);
        }
        if(StringUtils.isNotBlank(agencyDto.getOrgPosition())){
            wrapper.like("s_org_position",agencyDto.getOrgPosition(),SqlLike.RIGHT);
        }
        //根据区域Id查询
        if(StringUtils.isNotBlank(agencyDto.getRegionArea())){
            wrapper.eq("s_region_area",agencyDto.getRegionArea());
        }else{
            if(StringUtils.isNotBlank(agencyDto.getRegionCity())){
                wrapper.eq("s_region_city",agencyDto.getRegionCity());
            }else{
                if(StringUtils.isNotBlank(agencyDto.getRegionProvince())){
                    wrapper.eq("s_region_province",agencyDto.getRegionProvince());
                }
            }
        }
        wrapper.orderBy("t_create_date",false);
        return new ResultBean(ResultEnum.SUCCESS,this.selectPage(agencyDto.getPage(),wrapper));
    }

    @Override
    public ResultBean<Agency> addAgency(AgencyDto agencyDto) {
        // 判重
        List<Agency> agencyList = this.getListByCondition(agencyDto);
        if(agencyList != null && agencyList.size() > 0){
            Assert.isTrue(false,"所在地区已存在该社区");
        }

        //1、先建立社区机构，默认挂在根机构下
        Organization org = this.orgFeign.getOrgById(1L);
        OrgDto orgDto = new OrgDto();
        orgDto.setPid(org.getId());
        orgDto.setPosition(org.getPosition());
        orgDto.setOrgType(4); //社区类型
        orgDto.setStatus(OperationConstant.ZERO);
        orgDto.setIsEnable(1);
        orgDto.setCode("O");
        orgDto.setFlag(OperationConstant.ONE);

        orgDto.setName(agencyDto.getAgencyName());
        orgDto.setRegionProvince(agencyDto.getRegionProvince());
        orgDto.setRegionCity(agencyDto.getRegionCity());
        orgDto.setRegionArea(agencyDto.getRegionArea());
        orgDto.setRegion(agencyDto.getRegion());
        orgDto.setRegionStreet(agencyDto.getRegionStreet());
        orgDto.setPersonName(agencyDto.getPersonName());
        orgDto.setPersonTel(agencyDto.getPersonPhone());

        ResultBean<Organization> resultBean = this.orgFeign.addEntity(orgDto);
        if(null != resultBean && resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())) {
            //2、再建立社区资源，orgpo跟社区机构的orgPosition保持一致
            // 有ID回，
            Organization agencyOrg = resultBean.getData();
            agencyDto.setOrgPid(agencyOrg.getId());
            agencyDto.setOrgName(agencyOrg.getName());
            agencyDto.setId(agencyOrg.getId());
            agencyDto.setOrgPosition(agencyOrg.getPosition());
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                agencyDto.setCreateDate(df.parse(df.format(new Date())));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            this.insert(agencyDto);
            return new ResultBean<>(ResultEnum.SUCCESS, agencyDto);
        }else{
            return new ResultBean<>(ResultEnum.ERROR, agencyDto);
        }

    }

    @Override
    public Agency findAgencyById(Long id) {
        return this.selectById(id);
    }

    @Override
    public ResultBean<Boolean> updateAgency(AgencyDto agencyDto) {
        return new ResultBean<>(ResultEnum.SUCCESS,this.updateById(agencyDto));
    }

    @Override
    public ResultBean<Boolean> deleteAgency(AgencyDto agencyDto) {
        return new ResultBean<>(ResultEnum.SUCCESS,this.deleteById(agencyDto.getId()));
    }

    @Override
    public ResultBean<Boolean> deleteAgencyList(AgencyDto agencyDto) throws Exception {
        return null;
    }

    @Override
    public ResultBean<Agency> findListByPosition(AgencyDto agencyDto) {
        return null;
    }

    @Override
    public List<Agency> getListByCondition(AgencyDto agencyDto) {
        Wrapper<Agency> wrapper = new EntityWrapper<>();
        //按区来查
        if(StringUtils.isNotBlank(agencyDto.getAgencyArea())){
            wrapper.eq("s_region_area", agencyDto.getRegionArea());
        }
        //按社区名称
        if(StringUtils.isNotBlank(agencyDto.getAgencyName())){
            wrapper.eq("s_agency_name", agencyDto.getAgencyName());
        }
        List<Agency> agencyList = this.baseMapper.selectList(wrapper);
        return agencyList;
    }
}
