package com.sptg.estate.biz.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.estate.api.dto.AgencyDto;
import com.sptg.estate.api.entity.Agency;
import com.sptg.estate.api.feigns.AgencyFeign;
import com.sptg.estate.biz.service.AgencyServiceInterface;
import com.sptg.estate.biz.service.ResidentialServiceInterface;
import com.sptg.permissions.api.dto.OrgDto;
import com.sptg.permissions.api.entity.Area;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.entity.Street;
import com.sptg.permissions.api.feigns.AreaFeign;
import com.sptg.permissions.api.feigns.OrgFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created By lidexi in 2021/1/8
 * 社区控制器
 **/
@RestController
public class AgencyController implements AgencyFeign {

    private final AgencyServiceInterface agencyServiceInterface;
    private final OrgFeign orgFeign;

    @Autowired
    public AgencyController(AgencyServiceInterface agencyServiceInterface, OrgFeign orgFeign) {
        this.agencyServiceInterface = agencyServiceInterface;
        this.orgFeign = orgFeign;
    }

    @Override
    @PostMapping(value = "/agency/showAgencyList")
    public ResultBean<Page<Agency>> showAgencyList(@RequestBody AgencyDto agencyDto) {
        return this.agencyServiceInterface.showAgencyList(agencyDto);
    }

    @Override
    @PostMapping(value = "/agency/addAgency")
    public ResultBean<Agency> addAgency(@RequestBody AgencyDto agencyDto) {
        return this.agencyServiceInterface.addAgency(agencyDto);
    }

    @Override
    @GetMapping(value = "/agency/findAgencyById/{id}")
    public Agency findAgencyById(@PathVariable("id") Long id) {
        return this.agencyServiceInterface.findAgencyById(id);
    }

    @Override
    @PostMapping(value = "/agency/updateAgency")
    public ResultBean<Boolean> updateAgency(@RequestBody AgencyDto agencyDto) {
        return this.agencyServiceInterface.updateAgency(agencyDto);
    }

    @Override
    @PostMapping(value = "/agency/deleteAgency")
    public ResultBean<Boolean> deleteAgency(@RequestBody AgencyDto agencyDto) {
        return this.agencyServiceInterface.deleteAgency(agencyDto);
    }

    @Override
    @PostMapping(value = "/agency/deleteAgencyList")
    public ResultBean<Boolean> deleteAgencyList(@RequestBody AgencyDto agencyDto) {
        return null;
    }

    @Override
    @PostMapping(value = "/agency/findListByPosition")
    public ResultBean<Agency> findListByPosition(@RequestBody AgencyDto agencyDto) {
        return null;
    }

    @Override
    @PostMapping(value = "agency/getListByCondition")
    public List<Agency> getListByCondition(@RequestBody AgencyDto agencyDto) {
        List<Agency> agencyList = this.agencyServiceInterface.getListByCondition(agencyDto);
        return agencyList;
    }


//    @PostMapping(value = "agency/addOrgByAgency")
//    public ResultBean<Boolean> addOrgByAgency() {
//        AgencyDto agencyDto = new AgencyDto();
//        Page<Agency> page = new Page<>();
//        page.setSize(3000);
//        page.setCurrent(1);
//        agencyDto.setPage(page);
//        ResultBean<Page<Agency>> pageResultBean = this.agencyServiceInterface.showAgencyList(agencyDto);
//        List<Agency> records = pageResultBean.getData().getRecords();
//        for(int i = 0;i<records.size();i++){
//            Agency agency = records.get(i);
//            OrgDto organization = new OrgDto();
//            organization.setIsEnable(1);
//            organization.setClientCount(0);
//            organization.setDeptCount(0);
//            organization.setLayerNumber(2);
//            organization.setParentName("机构总管理");
//            organization.setPid(1L);
//            organization.setAddress(agency.getAddress());
//            organization.setOrgType(4);
//            organization.setPosition("0/1/");
//            organization.setName(agency.getAgencyName());
//            organization.setPersonName(agency.getPersonName());
//            organization.setPersonTel(agency.getPersonPhone());
//            organization.setRegionArea(agency.getRegionArea());
//            organization.setRegionCity(agency.getRegionCity());
//            organization.setRegionProvince(agency.getRegionProvince());
//            organization.setRegionStreet(agency.getRegionStreet());
//            organization.setRootOrgId(1L);
//            organization.setCreateDate(agency.getCreateDate());
//            organization.setRegion(agency.getProvinceName()+"-"+agency.getCityName()+"-"+agency.getAreaName());
//            organization.setStatus(0);
//            ResultBean<Organization> organizationResultBean = this.orgFeign.addEntity(organization);
//            Organization organization1 = organizationResultBean.getData();
//            agency.setOrgPid(organization1.getId());
//            agency.setOrgName(organization1.getName());
//            organization1.setCode("O"+organization1.getId());
//            this.orgFeign.updateEntity(organization1);
//            this.agencyServiceInterface.updateById(agency);
//        }
//        return null;
//    }

    @PostMapping(value = "/agency/setOrgPositon")
    public void setOrgPostion(){
        AgencyDto agencyDto = new AgencyDto();
        Page<Agency> page = new Page<>();
        page.setSize(3000);
        page.setCurrent(1);
        agencyDto.setPage(page);
        ResultBean<Page<Agency>> pageResultBean = this.agencyServiceInterface.showAgencyList(agencyDto);
        List<Agency> records = pageResultBean.getData().getRecords();
        for(int i=0;i<records.size();i++){
            Agency agency = records.get(i);
            Organization organization = this.orgFeign.getOrgById(agency.getOrgPid());
            agency.setOrgPosition(organization.getPosition());
            this.agencyServiceInterface.updateById(agency);
        }
    }


}
