package cn.superdesk.uniorder.dubbo.consumer.unified;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.JwtUtil;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.setting.UnifiedService;
import cn.superdesk.uniorder.dto.param.setting.UnifiedServicePartGoodsTransportParam;
import cn.superdesk.uniorder.dto.param.setting.UnifiedServicePartOrderStateParam;
import cn.superdesk.uniorder.dto.param.setting.UnifiedServicePartPayTypeParam;
import cn.superdesk.uniorder.dto.param.setting.UnifiedServicePartServiceParam;
import cn.superdesk.uniorder.dto.vo._function.select.SelectOptionUnifiedService;
import cn.superdesk.uniorder.dubbo.consumer._permission.PermissionDubboCustomerService;
import cn.superdesk.uniorder.dubbo.facade.setting.DubboUnifiedServiceService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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


/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/5/17 上午11:02
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/5/17 上午11:02
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class UnifiedServiceDubboConsumerService {

	@Reference(version = "1.0.0", timeout = 30000, retries = -1)
	private DubboUnifiedServiceService dubboUnifiedServiceService;

	@Autowired
	private PermissionDubboCustomerService permissionDubboCustomerService;

	/**
	 * 服务类型-下拉
	 *
	 * @param token
	 * @return
	 */
	public List<UnifiedService> getAsService(String token) {

		return dubboUnifiedServiceService.getAsService();
	}

	/**
	 * 订单类型-下拉
	 *
	 * @param token
	 * @return
	 */
	public List<UnifiedService> getAsOrderType(String token) {

		// 如果是 统一订单用户 没有这个就是全部

		// 如果是 服务之窗用户 没有这个就是没有
//		1.若该运营商无订单类型的业务数据权限,即为空显示,视为无任何订单类型业务数据权限;
//		2.若该运营商有订单类型的业务数据权限,即显示对应订单类型,无权限不显示;

		// 算了 这个逻辑不在这里写 放到 permissionDubboCustomerService.getAsOrderType

//		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		return permissionDubboCustomerService.getAsOrderType(token);
	}

	public R<List<UnifiedService>> getPage(String token, Map<String, Object> params, int pageNum, int pageSize) {

		return dubboUnifiedServiceService.getPage(params, pageNum, pageSize);
	}

	public UnifiedService addObj(String token, UnifiedService model) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		model.setCreateUserId(loginSysUser.getSysUserId());
		model.setCreateTime(DateUtil.date());
		model.setEnabled(1);

		return dubboUnifiedServiceService.addObj(model);
	}

	public UnifiedService updateObj(String token, UnifiedService model) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		model.setUpdateUserId(loginSysUser.getSysUserId());
		model.setUpdateTime(DateUtil.date());

		return dubboUnifiedServiceService.updateObj(model);
	}

	public UnifiedService updateUnifiedServicePartGoodsTransport(String token, UnifiedServicePartGoodsTransportParam partGoodsTransportParam) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		partGoodsTransportParam.setUpdateUserId(loginSysUser.getSysUserId());
		partGoodsTransportParam.setUpdateTime(DateUtil.date());

		return dubboUnifiedServiceService.updateUnifiedServicePartGoodsTransport(partGoodsTransportParam);
	}

	public UnifiedService updateUnifiedServicePartOrderState(String token, UnifiedServicePartOrderStateParam partOrderParam) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		partOrderParam.setUpdateUserId(loginSysUser.getSysUserId());
		partOrderParam.setUpdateTime(DateUtil.date());

		return dubboUnifiedServiceService.updateUnifiedServicePartOrderState(partOrderParam);
	}

	public UnifiedService updateUnifiedServicePartPayType(String token, UnifiedServicePartPayTypeParam partOrderParam) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		partOrderParam.setUpdateUserId(loginSysUser.getSysUserId());
		partOrderParam.setUpdateTime(DateUtil.date());

		return dubboUnifiedServiceService.updateUnifiedServicePartPayType(partOrderParam);
	}


	public UnifiedService updateUnifiedServicePartService(String token, UnifiedServicePartServiceParam partServiceParam) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		partServiceParam.setUpdateUserId(loginSysUser.getSysUserId());
		partServiceParam.setUpdateTime(DateUtil.date());

		return dubboUnifiedServiceService.updateUnifiedServicePartService(partServiceParam);
	}


	public UnifiedService findById(Long id) {

		return dubboUnifiedServiceService.findById(id);
	}

	public void delObj(Long id) {

		dubboUnifiedServiceService.delObj(id);
	}

	public List<SelectOptionUnifiedService> getSelectByVirt(Map<String, Object> params) {

		return dubboUnifiedServiceService.getSelectByVirt(params);
	}

	public UnifiedService findByCode(String code) {
		return dubboUnifiedServiceService.findByCode(code);
	}
}
