/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.param.ShopAuditingParam;
import com.yami.shop.bean.dto.ShopAuditingInfoDto;
import com.yami.shop.bean.enums.AuditStatus;
import com.yami.shop.bean.enums.OfflineHandleEventType;
import com.yami.shop.bean.enums.ShopStatus;
import com.yami.shop.bean.model.OfflineHandleEvent;
import com.yami.shop.bean.model.ShopAuditing;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.param.AuditingInfoParam;
import com.yami.shop.bean.param.OfflineHandleEventAuditParam;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.platform.util.SecurityUtils;
import com.yami.shop.service.OfflineHandleEventService;
import com.yami.shop.service.ShopAuditingService;
import com.yami.shop.service.ShopDetailService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;
import com.yami.shop.common.util.R;

/**
 * 商家审核信息管理实现
 *
 * @author Dwl
 * @version 1.0.0
 * @since 2019-09-19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/shopAuditing")
public class ShopAuditingController {

    private final ShopAuditingService shopAuditingService;
    private final ShopDetailService shopDetailService;
    private final OfflineHandleEventService offlineHandleEventService;

    /**
     * 分页查询
     *
     * @param page              分页对象
     * @param auditingInfoParam 商家审核信息
     * @return 分页数据
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('shop:shopAuditing:page')")
    public R<IPage<ShopAuditingInfoDto>> getShopAuditingPage(PageParam<ShopAuditingInfoDto> page, AuditingInfoParam auditingInfoParam) {
        return R.SUCCESS(shopAuditingService.auditingInfoList(page, auditingInfoParam));
    }

    /**
     * 根据审核id查询详情信息
     */
    @GetMapping("/shopDetail/{shopId}")
    @PreAuthorize("@pms.hasPermission('shop:shopAuditing:info')")
    public R<ShopDetail> auditingDetail(@PathVariable Long shopId) {
        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
        return R.SUCCESS(shopDetail);
    }

    /**
     * 根据审核id查询详情信息
     */
    @GetMapping("/{shopId}")
    public R<ShopAuditing> getShopAuditing(@PathVariable Long shopId) {
        ShopAuditing shopAuditing = shopAuditingService.getOne(new LambdaQueryWrapper<ShopAuditing>().eq(ShopAuditing::getShopId, shopId));
        if (Objects.isNull(shopAuditing)){
            shopAuditing = new ShopAuditing();
        }
        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
        shopAuditing.setShopDetail(shopDetail);
        return R.SUCCESS(shopAuditing);
    }

    /**
     * 审核信息 第一次审核商家
     */
    @SysLog("审核商家信息")
    @PutMapping("/audit/{shopId}")
    @PreAuthorize("@pms.hasPermission('shop:shopAuditing:audit')")
    public R<Void> audit(@PathVariable Long shopId, @Valid @RequestBody ShopAuditingParam shopAuditingParam) {

        ShopAuditing shopAuditing = shopAuditingService.getOne(new LambdaQueryWrapper<ShopAuditing>().eq(ShopAuditing::getShopId, shopId));
        if (Objects.equals(shopAuditing.getStatus(), AuditStatus.SUCCESSAUDIT.value())) {
            throw new GlobalException("商家已经审核成功，无需再次审核");
        }
        shopAuditing.setUpdateTime(new Date());
        shopAuditing.setRemarks(shopAuditingParam.getRemarks());
        shopAuditing.setStatus(shopAuditingParam.getStatus());
        shopAuditing.setAuditorId(SecurityUtils.getSysUser().getUserId());
        shopDetailService.audit(shopAuditing);

        shopDetailService.removeShopDetailCacheByShopId(shopId);

        return R.SUCCESS();
    }

    /**
     * 新建店铺
     */
    @PostMapping

    public R<ShopDetail> insertDetail(@RequestBody ShopDetail shopDetail) {
        shopDetailService.insertDetail(shopDetail);
        return R.SUCCESS(shopDetail);
    }

    /**
     * 校验账号
     */
    @GetMapping("/checkMobile")
    public R<Boolean> checkMobile(@RequestParam("mobile") String mobile,@RequestParam("shopId")Long shopId) {
        Boolean isTrue = true;
        if (Objects.isNull(shopId)){
            shopId = 0L;
        }
        int count = shopDetailService.checkMobile(mobile,Long.valueOf(shopId));
        if (count > 0){
            isTrue = false;
        }
        return R.SUCCESS(isTrue);
    }


    /**
     * 重置密码或者修改账号
     */
    @PutMapping("/updatePasswordOrMobile")
    public R<Boolean> updatePasswordOrMobile(@RequestBody ShopDetail shopDetail) {
        shopDetailService.updatePasswordOrMobile(shopDetail.getShopId(),shopDetail.getPassword(),shopDetail.getMobile());
        return R.SUCCESS();
    }

//    /**
//     * 下线店铺
//     */
//    @SysLog("下线店铺信息")
//    @PutMapping("/lowerShelfShop")
//    @PreAuthorize("@pms.hasPermission('shop:shopAuditing:lowerShelfShop')")
//    public ResponseEntity<Boolean> lowerShelfShop(@RequestBody OfflineHandleEvent offlineHandleEvent) {
//        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(offlineHandleEvent.getHandleId());
//        if (shopDetail == null) {
//            throw new GlobalException("店铺信息不存在");
//        }
//        if (!Objects.equals(shopDetail.getShopStatus(), ShopStatus.OPEN.value())) {
//            throw new GlobalException("店铺不处于营业状态,不能进行下线");
//        }
//        shopDetail.setShopStatus(ShopStatus.STOP.value());
//        return ResponseEntity.ok(shopDetailService.updateById(shopDetail));
//    }


}
