package com.bjtdba.merchant.controller;

import com.bjtdba.entity.merchant.MerchantIntention;
import com.bjtdba.merchant.service.MerchantSettleInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.util.Map;


@RestController
@RequestMapping("merchant_service/merchantSettleInService")
public class MerchantSettleInController {


    @Autowired
    private MerchantSettleInService merchantSettleInService;


    /**
     * 创建支付订单
     */
    @PostMapping("/insterMerchantOrder")
    public Object MerchantOrder(@RequestBody Map<String,String> map) throws ParseException {
        return merchantSettleInService.MerchantOrder(map.get("uid"));
    }
    /**
     * 1、用户登录成功以后调用该接口，判断用户进入到哪一环节
     * 2、没有交钱
     * 3、交钱了
     */
    @PostMapping("/selectMerchant")
    public Object selectMerchant(@RequestBody Map<String,String> map) throws ParseException {
        return merchantSettleInService.selectMerchant(map.get("uid"));
    }
    /**
     * 激活邀请码接口
     * 1、判断是否有该码
     * 2、插入一条数据
     * code
     * uid   增加商户入住时间
     */
    @PostMapping("/updateActivationCode")
    public Object updateActivationCode(@RequestBody Map<String,String> map) throws ParseException {
        return merchantSettleInService.updateActivationCode(map.get("code"),map.get("uid"));
    }
    /**
     * 银行
     */
    @PostMapping("/settlementBank")
    public Object settlementBank(@RequestBody Map<String,String> map){
        return merchantSettleInService.selectSettlementBank(map.get("bank_name"));
    }
    /**
     * 地址接口
     */
    @PostMapping("/addressList")
    public Object addressList(@RequestBody Map<String,String> map){
        return merchantSettleInService.selectAddressList(Integer.parseInt(map.get("id")),map.get("code"));

    }
    /**
     * 图片上传
     * @param file
     * @param uid
     */
    @PostMapping("/uploadPicture")
    public Object uploadPicture(@RequestParam(value="file",required=false) MultipartFile file, String uid){
        return merchantSettleInService.uploadPicture(file, uid);
    }
    /**
     * 1、商家类型，企业入驻、个人入驻
     * 测试
     */
    @PostMapping("/merchaSettlen")
    @Transactional(rollbackFor=Exception.class)
    public Object merchaSettlen(@RequestBody MerchantIntention MerchantIntention) throws Exception {
        return merchantSettleInService.insertMerchantSettlen(MerchantIntention);
    }
    /**
     * 更具商户ID查询商户提交的数据
     */
    @PostMapping("/selectMerchaSettlen")
    public Object selectMerchaSettlen(@RequestBody Map<String,String> map){
        return merchantSettleInService.selectMerchaSettlen(map.get("mer_id"));
    }
    /**
     * 修改数据的接口
     */
    @PostMapping("/updateMerchaSettlen")
    @Transactional(rollbackFor=Exception.class)
    public Object updateMerchaSettlen(@RequestBody MerchantIntention MerchantIntention) throws Exception   {
        return merchantSettleInService.updateMerchaSettlen(MerchantIntention);
    }

    /**
     * 生成二维码
     */
   /* @PostMapping("/demo")
    public void demo() throws Exception {
        *//**
         * 1、生成时间戳   充当二维码的code以及名称
         * 2、将本地的二维码上传到远程数据库
         * 3、将二维码的地址以及uid、mer_id、code、二维码的url 保存到数据库中  与商户产生绑定
         * 3、将本的二维码删除
         *//*

        // 存放在二维码中的内容    http://app.bjtdba.com/offlinepay/index.html?id=164888564678229691#/
        long timeNew = System.currentTimeMillis(); // 13位数的时间戳    这个也是code
        String text = "http://app.bjtdba.com/offlinepay/index.html?id="+timeNew;
        // 嵌入二维码的图片路径
        String imgPath = "";
        // 生成的二维码的路径及名称
        String destPath = "/Users/hanyuxing/qrcode/"+timeNew+".jpg";
        //生成二维码
        QRCodeUtil.encode(text, imgPath, destPath, true);
        // 解析二维码
        String str = QRCodeUtil.decode(destPath);
        // 打印出解析出的内容
        System.out.println(str);
        //上传二维码到c d n
        File file = new File(destPath);
        FileInputStream input = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
        Map<String, String> qrCode = uploadDownload.qrCode(multipartFile);
        MerchantQrcode merchantQrcode = new MerchantQrcode();
        merchantQrcode.setUid(3720);
        merchantQrcode.setMer_id(741);
        merchantQrcode.setCode_id(timeNew+"");
        merchantQrcode.setQrcode_src(qrCode.get("url"));
        merchantQrcode.setShow_src("");
        merchantQrcodeDao.insterMerchantQrcode(merchantQrcode);
        deleteDir(new File("/Users/hanyuxing/qrcode"));
    }
    private boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            //递归删除目录中的子目录下
            for (int i=0;i<children.length;i++){
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空，可以删除
        return dir.delete();
    }*/
}
