package com.xkrs.controller;

import com.aliyuncs.exceptions.ClientException;
import com.xkrs.common.config.RedisUtil;
import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.common.tool.TokenUtil;
import com.xkrs.dao.SysUserDao;
import com.xkrs.model.entity.SysUserEntity;
import com.xkrs.model.qo.BankCardQo;
import com.xkrs.model.qo.BusinessQo;
import com.xkrs.service.MerchantSettlementService;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;

/**
 * @Author: XinYi Song
 * @Date: 2021/12/14 14:40
 */
@RestController
public class MerchantSettlementController {

    @Resource
    private MerchantSettlementService merchantSettlementService;

    @Resource
    private SysUserDao sysUserDao;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 商家入驻
     * @param
     * @param
     * @param
     * @param
     * @param
     * @return
     */
    @PostMapping("/merchantSettlement")
    public String merchantSettlement(@RequestBody Map map) throws IOException {
        Locale locale = LocaleContextHolder.getLocale();
        String businessName = (String) map.get("businessName");
        String businessPro = (String) map.get("businessPro");
        String businessCity = (String) map.get("businessCity");
        String businessCountry = (String) map.get("businessCountry");
        String businessAddress = (String) map.get("businessAddress");
        String businessPhone = (String) map.get("businessPhone");
        String businessPhoto = (String) map.get("businessPhoto");
        String businessLicense = (String) map.get("businessLicense");
        Double businessDiscount = (Double) map.get("businessDiscount");
        String headIdentifier = (String) map.get("headIdentifier");
        List<Object> bankCardQos = (List<Object>) map.get("bankCardQos");
        String verificationCode = (String) map.get("verificationCode");
        BusinessQo businessQo = new BusinessQo();
        businessQo.setBusinessName(businessName);
        businessQo.setBusinessPro(businessPro);
        businessQo.setBusinessCity(businessCity);
        businessQo.setBusinessCountry(businessCountry);
        businessQo.setBusinessAddress(businessAddress);
        businessQo.setBusinessPhone(businessPhone);
        businessQo.setBusinessPhoto(businessPhoto);
        businessQo.setBusinessLicense(businessLicense);
        businessQo.setBusinessDiscount(businessDiscount);
        businessQo.setHeadIdentifier(headIdentifier);
        String o = (String) redisUtil.get(businessPhone);
        if("".equals(o) || o == null){
            return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG,"请先发送验证码！",locale);
        }
        if(!redisUtil.get(businessPhone).equals(verificationCode)){
            return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG,"验证码错误，请重新输入！",locale);
        }
        return merchantSettlementService.merchantSettlement(businessQo,bankCardQos);
    }

    /**
     * 根据商家入驻状态查询待审核，审核通过，审核不通过商家的信息
     * @param type
     * @return
     */
    @GetMapping("/findAllByBusinessType")
    @PreAuthorize("hasAnyAuthority('auth_administor')")
    public String findAllByBusinessType(@RequestParam("type") String type){
        return merchantSettlementService.findAllByBusinessType(type);
    }

    /**
     * 商家查询自己的入驻信息
     * @param token
     * @return
     */
    @GetMapping("/findBusinessById")
    public String findBusinessById(@RequestHeader(value="Authorization") String token){
        String tokenUserName = TokenUtil.getTokenUserName(token);
        SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
        return merchantSettlementService.findBusinessById(sysUserEntity.getId());
    }

    /**
     * 市级或区县级查看入驻商家的信息
     * @param map
     * @return
     */
    @PostMapping("/selectAllBusiness")
    @PreAuthorize("hasAnyAuthority('auth_city','auth_county')")
    public String selectAllBusiness(@RequestBody Map map,@RequestHeader(value="Authorization") String token){
        String businessCity = (String) map.get("businessCity");
        String businessCountry = (String) map.get("businessCountry");
        String businessName = (String) map.get("businessName");
        return merchantSettlementService.selectAllBusiness(businessCity,businessCountry,businessName);
    }

    /**
     * 对商家入驻进行审核（通过）
     * @param map
     * @return
     */
    @PostMapping("/updatePassBusiness")
    @PreAuthorize("hasAnyAuthority('auth_administor')")
    public String updatePassBusiness(@RequestBody Map map,@RequestHeader(value="Authorization") String token) throws ClientException {
        Integer id = (Integer) map.get("id");
        String username = (String) map.get("username");
        String province = (String) map.get("province");
        String city = (String) map.get("city");
        String county = (String) map.get("county");
        return merchantSettlementService.updatePassBusiness(id,username,province,city,county);
    }

    /**
     * 拒绝商家入驻的请求（审核不通过）
     * @param map
     * @return
     * @throws ClientException
     */
    @PostMapping("/updateNotPassBusiness")
    @PreAuthorize("hasAnyAuthority('auth_administor')")
    public String updateNotPassBusiness(@RequestBody Map map,@RequestHeader(value="Authorization") String token) throws ClientException {
        Integer id = (Integer) map.get("id");
        String phone = (String) map.get("phone");
        return merchantSettlementService.updateNotPassBusiness(id,phone);
    }

    /**
     * 总部查看要审核的商家的信息
     * @param map
     * @return
     */
    @PostMapping("/selectDefaultBusiness")
    @PreAuthorize("hasAnyAuthority('auth_administor')")
    public String selectDefaultBusiness(@RequestBody Map map,@RequestHeader(value="Authorization") String token){
        String businessCity = (String) map.get("businessCity");
        String businessCountry = (String) map.get("businessCountry");
        String businessName = (String) map.get("businessName");
        return merchantSettlementService.selectDefaultBusiness(businessCity,businessCountry,businessName);
    }
}
