package com.bonus.core.allocation.advise.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.core.allocation.advise.dto.BasicsComplaintDTO;
import com.bonus.core.allocation.advise.mapper.BasicsPlaintMapper;
import com.bonus.core.allocation.advise.model.BasicsComplaint;
import com.bonus.core.allocation.advise.param.AppletPlaintParam;
import com.bonus.core.allocation.advise.service.BasicsPlaintService;
import com.bonus.core.allocation.advise.vo.AppletPlaintVO;
import com.bonus.core.customer.api.CustCasualApi;
import com.bonus.i18n.I18n;
import com.bonus.utils.AesEncryptUtil;
import com.bonus.utils.LeBeanUtil;
import com.bonus.utils.id.Id;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.lang.invoke.SerializedLambda;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class BasicsPlaintServiceImpl extends ServiceImpl<BasicsPlaintMapper, BasicsComplaint> implements BasicsPlaintService {
   @Autowired
   @Lazy
   private CustCasualApi custCasualApi;

//   @Autowired
//   private AesEncryptUtil aesEncryptUtil;
//   @ApiModelProperty
//   @Autowired
//   private AllocCanteenApi allocCanteenApi;

   public void postPlaint(BasicsComplaintDTO complaint) {
      Long custId = this.custCasualApi.getCustIdByOpenidAndSourceType(complaint.getOpenid(), complaint.getSourceType());
      if (ObjectUtil.isNull(custId)) {
         throw new ServiceException(I18n.getMessage("notice_applet_plaint_null_custid_exception", new Object[0]));
      } else {
         BasicsComplaint basicsComplaint = new BasicsComplaint();
         basicsComplaint.setCustId(custId);
         complaint.setMobile(AesEncryptUtil.aesDecode(complaint.getMobile()));
         LeBeanUtil.copyProperties(complaint, basicsComplaint);
         basicsComplaint.setComplaintId(Id.next());
         if (CollectionUtil.isNotEmpty(complaint.getComplaintPictureList())) {
            basicsComplaint.setComplaintPicture(String.join(",", complaint.getComplaintPictureList()));
         }

         ((BasicsPlaintMapper)this.baseMapper).insert(basicsComplaint);
      }
   }

   public List<AppletPlaintVO> queryPlaints(String openId, Integer sourceType) {
      Long custId = this.custCasualApi.getCustIdByOpenidAndSourceType(openId, sourceType);
      if (ObjectUtil.isNull(custId)) {
         throw new ServiceException(I18n.getMessage("notice_applet_plaint_null_custid_exception", new Object[0]));
      } else {
         List<AppletPlaintParam> appletPlaintParams = ((BasicsPlaintMapper)this.baseMapper).queryPlaints(custId, sourceType);
         List<AppletPlaintVO> appletPlaintVOList = new ArrayList();
         appletPlaintParams.forEach((appletPlaintParam) -> {
            AppletPlaintVO appletPlaint = new AppletPlaintVO();
            appletPlaintParam.setMobile(AesEncryptUtil.aesEncrypt(appletPlaintParam.getMobile()));
            LeBeanUtil.copyProperties(appletPlaintParam, appletPlaint);
            appletPlaint.setComplaintPictureList(new ArrayList());
            if (StrUtil.isNotBlank(appletPlaintParam.getComplaintPicture())) {
               appletPlaint.setComplaintPictureList(Arrays.asList(appletPlaintParam.getComplaintPicture().split(",")));
            }

            appletPlaintVOList.add(appletPlaint);
         });
         return appletPlaintVOList;
      }
   }

//   public Page<BasicsComplaintPageVO> getPlaintByPage(String content) {
//      Map<String, JSONObject> contentMap = (Map)JSON.parseObject(content, Map.class);
//      PageDTO pageDTO = (PageDTO)((JSONObject)contentMap.get("page")).toJavaObject(PageDTO.class);
//      BasicsPlaintPageDTO basicsPlaintPageDTO = (BasicsPlaintPageDTO)((JSONObject)contentMap.get("object")).toJavaObject(BasicsPlaintPageDTO.class);
//      if (StrUtil.isNotBlank(basicsPlaintPageDTO.getInformContent())) {
//         basicsPlaintPageDTO.setInformContent(LeBeanUtil.fieldLikeHandle(basicsPlaintPageDTO.getInformContent()));
//      }
//
//      return this.getBasicsComplaintPageVOPage(pageDTO, basicsPlaintPageDTO);
//   }
//
//   private Page<BasicsComplaintPageVO> getBasicsComplaintPageVOPage(PageDTO pageDTO, BasicsPlaintPageDTO basicsPlaintPageDTO) {
//      if (CollUtil.isNotEmpty(basicsPlaintPageDTO.getAreaIdList()) && ObjectUtil.isEmpty(basicsPlaintPageDTO.getCanteenIds())) {
//         basicsPlaintPageDTO.setCanteenIds(this.allocCanteenApi.getCanteenIdListByAreaIdList((List)basicsPlaintPageDTO.getAreaIdList()));
//      }
//
//      Page<BasicsComplaintPageVO> plaintPage = ((BasicsPlaintMapper)this.baseMapper).selectPlaintPage(new Page(pageDTO.getCurrent(), pageDTO.getSize()), basicsPlaintPageDTO);
//      plaintPage.getRecords().forEach((record) -> {
//         String sourceName = SourceTypeEnum.getDesc(record.getSourceType());
//         record.setSourceName(sourceName);
//         record.setMobile(this.aesEncryptUtil.aesEncrypt(record.getMobile()));
//         if (StrUtil.isNotBlank(record.getComplaintPicture())) {
//            record.setComplaintPictureList(Arrays.asList(record.getComplaintPicture().split(",")));
//         }
//
//      });
//      return plaintPage;
//   }
//
//   public void replyComplaint(BasicsReplyComplaintDTO basicsReplyComplaintDTO) {
//      BasicsComplaint basicsComplaint = new BasicsComplaint();
//      basicsComplaint.setReplyContent(basicsReplyComplaintDTO.getReplyContent());
//      basicsComplaint.setReplyState(ReplyStateEnum.REPLY_YES.getKey());
//      ((BasicsPlaintMapper)this.baseMapper).update(basicsComplaint, (Wrapper)Wrappers.lambdaQuery(BasicsComplaint.class).eq(BasicsComplaint::getComplaintId, basicsReplyComplaintDTO.getComplaintId()));
//   }
//
//   public Page<BasicsComplaintPageVO> pagePlaintForApp(BasicsPlaintPageDTO param) {
//      PageDTO pageDTO = new PageDTO();
//      pageDTO.setSize(param.getSize());
//      pageDTO.setCurrent(param.getCurrent());
//      if (StrUtil.isNotBlank(param.getInformContent())) {
//         param.setInformContent(LeBeanUtil.fieldLikeHandle(param.getInformContent()));
//      }
//
//      return this.getBasicsComplaintPageVOPage(pageDTO, param);
//   }
//
//   // $FF: synthetic method
//   private static Object $deserializeLambda$(SerializedLambda lambda) {
//      switch (lambda.getImplMethodName()) {
//         case "getComplaintId":
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/allocation/advise/model/BasicsComplaint") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return BasicsComplaint::getComplaintId;
//            }
//         default:
//            throw new IllegalArgumentException("Invalid lambda deserialization");
//      }
//   }
}
