package com.bonus.core.customer.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.constant.LeConstants;
import com.bonus.core.common.base.BaseController;
import com.bonus.core.common.utils.AesEncryptUtil;
import com.bonus.core.common.utils.HeaderFetchUtil;
import com.bonus.core.customer.dto.AddCustAddrForAppDTO;
import com.bonus.core.customer.model.CustAddr;
import com.bonus.core.customer.service.CustAddrService;
import com.bonus.core.customer.service.CustCasualService;
import com.bonus.core.customer.service.CustInfoBusiness;
import com.bonus.domain.CustCasual;
import com.bonus.encrypt.RequiresGuest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.invoke.SerializedLambda;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.bonus.common.core.web.domain.AjaxResult.error;
import static com.bonus.common.core.web.domain.AjaxResult.success;

@RestController
@RequestMapping({"/custaddr"})
@Api(
   tags = {"customer-地址信息控制器"}
)
public class CustAddrController extends BaseController {
   private static final Logger log = LoggerFactory.getLogger(CustAddrController.class);

   @Resource
   private CustAddrService custAddrService;

   @Resource
   private CustCasualService custCasualService;

   @Resource
   private  AesEncryptUtil aesEncryptUtil;


   @ApiOperation("小程序查询地址信息")
   @PostMapping({"/queryCustAddrForApp"})
   public AjaxResult queryCustAddrForApp( @RequestBody  CustCasual custCasualDto) {
      AjaxResult ajaxResult = new AjaxResult();
     // CustCasual casual = (CustCasual)this.custCasualService.getOne((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery().select(new SFunction[]{CustCasual::getCustId}).eq(CustCasual::getOpenid, this.getOpenidByHeaders(headers))).eq(CustCasual::getSourceType, this.getSourceTypeByHeaders(headers)));
      CustCasual casual = this.custCasualService.getOne(Wrappers.lambdaQuery(CustCasual.class)
              .select(CustCasual::getCustId)
              .eq(CustCasual::getOpenid, custCasualDto.getOpenid())
              .eq(CustCasual::getSourceType, custCasualDto.getSourceType()));
      List<CustAddr> custAddrList = new ArrayList();
      if (ObjectUtil.isEmpty(casual)) {
         return success(custAddrList);
      } else {
        // custAddrList = this.custAddrService.list((Wrapper)((LambdaQueryWrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(CustAddr.class).eq(CustAddr::getCustId, casual.getCustId())).eq(CustAddr::getIfDel, LeConstants.COMMON_NO)).orderByDesc(CustAddr::getId));
         custAddrList = this.custAddrService.list(Wrappers.lambdaQuery(CustAddr.class)
                 .eq(CustAddr::getCustId, casual.getCustId())
                 .eq(CustAddr::getIfDel, LeConstants.COMMON_NO)
                 .orderByDesc(CustAddr::getId));
//         custAddrList.forEach((custAddr) -> {
//            custAddr.setMobile(this.aesEncryptUtil.aesEncrypt(custAddr.getMobile()));
//         });
         return success(custAddrList);
      }
   }



   @ApiOperation("小程序修改地址信息")
   @PostMapping({"/updateCustAddrForApp"})
   @RequiresGuest
   public AjaxResult updateCustAddrForApp( @RequestBody CustAddr custAddr) {
      if (custAddr.getId() == null) {
         log.error("[地址信息]小程序修改_上送参数错误,主键id不能为空");
         return error("主键id不能为空");
      } else {
//         custAddr.setOpenid(this.getOpenidByHeaders(headers));
//         custAddr.setMobile(this.aesEncryptUtil.aesDecode(custAddr.getMobile()));
         this.custAddrService.updateById(custAddr);
         return success();
      }
   }




   @ApiOperation("小程序设置默认地址")
   @PostMapping({"/updateDefaultAddrForApp"})
   @RequiresGuest
   public AjaxResult updateDefaultAddrForApp( @RequestBody CustAddr custAddr) {
      AjaxResult ajaxResult = new AjaxResult();
      if (custAddr.getAddrId() == null) {
         log.error("[地址信息]小程序设置默认地址_上送参数错误,地址id不能为空");
         return error("地址id不能为空");
      } else {
//         custAddr.setOpenid(this.getOpenidByHeaders(headers));
//         custAddr.setSourceType(this.getSourceTypeByHeaders(headers));
         this.custAddrService.updateDefaultAddrForApp(custAddr);
         return success();
      }
   }


   @ApiOperation("小程序新增地址信息")
   @PostMapping({"/addCustAddrForApp"})
   @RequiresGuest
   public AjaxResult addCustAddrForApp( @RequestBody AddCustAddrForAppDTO custAddr) {
//      custAddr.setOpenid(this.getOpenidByHeaders(headers));
//      custAddr.setSourceType(this.getSourceTypeByHeaders(headers));
//      custAddr.setCustId(HeaderFetchUtil.getCustId(headers));
//      this.custInfoBusiness.addCustAddrForApp(custAddr);
      return success();
   }



}
