package cn.superdesk.uniorder.dubbo.provider.organisation;

import cn.superdesk.data.extract.enums.opCenter.OpCenterBizPlatformEnum;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.organisation.UnifiedSupplier;
import cn.superdesk.uniorder.dto.param.organisation.SysSupplierQueryParam;
import cn.superdesk.uniorder.dto.param.organisation.UnifiedSupplierParam;
import cn.superdesk.uniorder.dto.param.organisation.UnifiedSupplierQueryCriteria;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.SlaveSupplierReceiveParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.SuperMallSupplierReceiveParam;
import cn.superdesk.uniorder.dto.vo.organisation.UnifiedSupplierVo;
import cn.superdesk.uniorder.dto.vo.sys.SysOrgVo;
import cn.superdesk.uniorder.dto.vo.sys.SysSelectVo;
import cn.superdesk.uniorder.dto.vo.sys.SysSupplierVo;
import cn.superdesk.uniorder.dubbo.facade.organisation.DubboSysSupplierService;
import cn.superdesk.uniorder.service.organisation.SysSupplierService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;


/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/4/27 上午10:57
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/4/27 上午10:57
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service(version = "1.0.0", retries = -1, timeout = 10000)
public class DubboSysSupplierServiceImpl implements DubboSysSupplierService {

    @Autowired
    private SysSupplierService unifiedSupplierService;


    /****************************************** extends BaseService 部分 start ******************************************/

    @Override
    public void addObj(UnifiedSupplierParam param) {

        UnifiedSupplier unifiedSupplier = AnBeanUtils.copy(param, UnifiedSupplier.class);
        unifiedSupplierService.insertSelective(unifiedSupplier);

    }

    @Override
    public void updateObj(UnifiedSupplierParam param) {

        UnifiedSupplier unifiedSupplier = AnBeanUtils.copy(param, UnifiedSupplier.class);
        unifiedSupplierService.updateByPrimaryKeySelective(unifiedSupplier);
    }

    @Override
    public void deleteObj(Long id) {
        unifiedSupplierService.deleteObj(id);
    }

    @Override
    public UnifiedSupplier findById(Long id) {
        return unifiedSupplierService.findById(id);
    }

    @Override
    public PageInfo<UnifiedSupplier> entityPageQuery(Map<String, Object> conditions, Integer pageNun, Integer pageSize) {
        return unifiedSupplierService.findByPaging(conditions, pageNun, pageSize);
    }

    @Override
    public List<UnifiedSupplier> findAll(Map<String, Object> queryMap) {

        return unifiedSupplierService.findAll(queryMap);
    }


    /****************************************** extends BaseService 部分   end ******************************************/


    @Override
    public int addUnifiedSupplier(UnifiedSupplierParam param, LoginSysUser loginSysUser) {

        UnifiedSupplier entity = AnBeanUtils.copy(param, UnifiedSupplier.class);

        return unifiedSupplierService.addUnifiedSupplier(entity, loginSysUser);
    }

    @Override
    public int updateUnifiedSupplier(UnifiedSupplierParam param, LoginSysUser loginSysUser) {

        UnifiedSupplier entity = AnBeanUtils.copy(param, UnifiedSupplier.class);
        return unifiedSupplierService.updateUnifiedSupplier(entity, loginSysUser);
    }

    @Override
    public int deleteUnifiedSupplier(Long id, LoginSysUser loginSysUser) {
        return unifiedSupplierService.deleteUnifiedSupplier(id, loginSysUser);
    }

    @Override
    public UnifiedSupplier findUnifiedSupplierById(Long id) {
        return unifiedSupplierService.findUnifiedSupplierById(id);
    }

    @Override
    public R<List<UnifiedSupplier>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {
        return unifiedSupplierService.pageQuery(conditions, pageNun, pageSize);

    }


    @Override
    public void updateObj(UnifiedSupplier oe) {
        unifiedSupplierService.updateByPrimaryKeySelective(oe);
    }

    @Override
    public void addObj(UnifiedSupplier oe) {
        unifiedSupplierService.insertSelective(oe);
    }

    @Override
    public R<List<UnifiedSupplierVo>> pageQuery(
            UnifiedSupplierQueryCriteria queryCriteria,
            LoginSysUser loginSysUser) {
        return unifiedSupplierService.pageQuery(queryCriteria, loginSysUser);
    }


    /****************************************** kafka 超级前台 企业采购 企业福利 start ******************************************/

    @Override
    public void kafkaMergeReceiveMerchant4SuperdeskMall(String message) {

        log.info("##################### kafkaMergeReceiveMerchant4SuperdeskMall start #####################\n原始:\n{}", message);

        try {

            SuperMallSupplierReceiveParam superMallSupplierReceiveParam = AnJsonUtils.toObject(message, SuperMallSupplierReceiveParam.class);
            log.info("\n转换:\n{}", AnJsonUtils.toJson(superMallSupplierReceiveParam));
            unifiedSupplierService.kafkaMergeReceiveMerchant4SuperdeskMall(superMallSupplierReceiveParam);

        } catch (Exception e) {

            log.error("kafkaMergeReceiveMerchant4SuperdeskMall 同步 错误 \n " + ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }
    }

    @Override
    public void kafkaDeleteReceiveMerchant4SuperdeskMall(String message) {

        SuperMallSupplierReceiveParam superMallSupplierReceiveParam = AnJsonUtils.toObject(message, SuperMallSupplierReceiveParam.class);

        // 逻辑删除
        unifiedSupplierService.kafkaDeleteReceiveMerchant4SuperdeskMall(superMallSupplierReceiveParam);
    }

    /****************************************** kafka 超级前台 企业采购 企业福利   end ******************************************/

    /****************************************** kafka 侨城汇专用 start ******************************************/

    @Override
    public void kafkaMergeReceiveMerchant4SuperdeskMallFLSC_QCH(String message) {


        log.info("##################### kafkaMergeReceiveMerchant4SuperdeskMallFLSC_QCH start #####################\n原始:\n{}", message);

        try {

            SlaveSupplierReceiveParam slaveSupplierReceiveParam = AnJsonUtils.toObject(message, SlaveSupplierReceiveParam.class);
            log.info("\n转换:\n{}", AnJsonUtils.toJson(slaveSupplierReceiveParam));

            unifiedSupplierService.kafkaMergeReceiveMerchant4SuperdeskMallFLSC_QCH(slaveSupplierReceiveParam);

        } catch (Exception e) {

            log.error("kafkaMergeReceiveMerchant4SuperdeskMallFLSC_QCH 同步 错误 \n " + ExceptionUtils.getMessage(e));

            e.printStackTrace();
        }
    }

    /**
     * 20200707 林志鹏说是没删除
     *
     * @param message
     */
    @Override
    @Deprecated
    public void kafkaDeleteReceiveMerchant4SuperdeskMallFLSC_QCH(String message) {
        unifiedSupplierService.kafkaDeleteReceiveMerchant4SuperdeskMallFLSC_QCH(message);
    }

    /****************************************** kafka 侨城汇专用   end ******************************************/

    @Override
    public void mergeReceiveSupplier4Qchjc(String message) {
        unifiedSupplierService.mergeReceiveSupplier4Qchjc(message);
    }

    @Override
    public R<List<SysSupplierVo>> findSupplierList(SysSupplierQueryParam param) {
        PageHelper.startPage(param.getPageNumber(), param.getPageSize());
        List<SysSupplierVo> list = unifiedSupplierService.findSupplierList(param);
        PageInfo<SysSupplierVo> page = new PageInfo<>(list);
        return new R<List<SysSupplierVo>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), list);
    }

    @Override
    public List<SysSelectVo> selectList(Map<String, Object> params) {
        return unifiedSupplierService.selectList(params);
    }
}
