package com.seeyon.ctp.rest.resources;

import com.seeyon.apps.addressbook.manager.AddressBookCustomerFieldInfoManager;
import com.seeyon.apps.addressbook.po.AddressBook;
import com.seeyon.apps.bipsync.aop.NcHrSynAspect;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.organization.OrgConstants;
import com.seeyon.ctp.organization.bo.V3xOrgAccount;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.util.Strings;
import org.apache.commons.logging.Log;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.Date;
import java.util.List;

@Path("/nc/oa")
@Produces({"application/json", "application/xml"})
public class NcSync2OaDbResource extends BaseResource{
    private static final Log log = CtpLogFactory.getLog(NcSync2OaDbResource.class);


    private AddressBookCustomerFieldInfoManager addressBookCustomerFieldInfoManager;
    private OrgManager orgManager;


    @Path("/sync")
    @GET
    public Response syncData(){
        try {
            NcHrSynAspect ncHrSynAspect = (NcHrSynAspect) AppContext.getBean("ncHrSynAspect");
            ncHrSynAspect.after(null,null);
            return success("同步成功！");
        }catch (Exception e){
            log.error("同步异常",e);
            return fail("同步失败！"+e.getMessage());
        }
    }

    @GET
    @Path("/init")
    @Produces({"application/json"})
    @Consumes({"application/json"})
    public Response init(@QueryParam("init") String init){
        int success = 0;
        boolean override = "1".equals(init);
        int fail = 0;
        try {
            V3xOrgAccount rootAccount = getOrgManager().getRootAccount();
            List<V3xOrgMember> memberList = getOrgManager().getAllMembers(rootAccount.getId(), true);
            for (V3xOrgMember member : memberList) {
                try {
                    AddressBook book = null;
                    book = getAddressBookCustomerFieldInfoManager().getByMemberId(member.getId());
                    if(null == book) {
                        book = new AddressBook();
                        book.setIdIfNew();
                        book.setCreateDate(new Date());
                        book.setUpdateDate(new Date());
                        book.setMemberId(member.getId());
                        book.setType(OrgConstants.ORGENT_TYPE.Member.name());
                        if(Strings.isNotBlank(member.getCode())) {
                            book.setExtAttr2(member.getCode());
                            getAddressBookCustomerFieldInfoManager().addAddressBook(book);
                        }
                    } else {
                        // 覆盖或者本身就是空
                        if((override || Strings.isBlank(book.getExtAttr2())) && Strings.isNotBlank(member.getCode())) {
                            book.setExtAttr2(member.getCode());
                            getAddressBookCustomerFieldInfoManager().updateAddressBook(book);
                        }
                    }
                    success++;
                } catch (Exception e) {
                    log.error("获取人员扩展字段发生错误：", e);
                    fail++;
                }
            }
        } catch (Exception e) {
            log.error("初始化字段异常：", e);
            return fail("初始化异常，请查看日志");
        }
        return success("成功：" + success + "个，失败：" + fail + "个", "初始化成功");
    }

    private AddressBookCustomerFieldInfoManager getAddressBookCustomerFieldInfoManager() {
        if(null == addressBookCustomerFieldInfoManager) {
            addressBookCustomerFieldInfoManager = (AddressBookCustomerFieldInfoManager) AppContext.getBean("addressBookCustomerFieldInfoManager");
        }
        return addressBookCustomerFieldInfoManager;
    }

    private OrgManager getOrgManager() {
        if(null == orgManager) {
            orgManager = (OrgManager) AppContext.getBean("orgManager");
        }
        return orgManager;
    }
}
