package view;

import dao.merchantsAbstractDAO;
import dao.specification.merchantsDAO;
import entities.Merchants;
import util.Toolset;

import java.util.Scanner;

//商家管理
public class MerchantsView {
    //商家数据库操作
    protected void merchantsDB(int chose) throws IllegalAccessException {
        switch (chose) {
            case 0 -> System.exit(0);
            case 1 -> CreateMerchant();
            case 2 -> DeleteMerchant();
            case 3 -> System.out.println("这个功能还在修复呢，小主先看看其它功能吧~");//UpdateMerchant();
            case 4 -> SearchMerchant(true);//查询所有
            case 5 -> SearchMerchant(false);//查询ID
        }
    }

    private void CreateMerchant() {//@TODO 输入格式化 不换行
        Scanner reader = new Scanner(System.in);
        merchantsAbstractDAO DML_insert = new merchantsDAO();

        System.out.println("请输入：店铺名字|地址|描述|电话");
        Merchants merchants = new Merchants
                (reader.nextLine(), reader.nextLine(), reader.nextLine(), reader.nextLine());

        int flag = DML_insert.insert(merchants);
        System.out.println(flag + "行受影响");
    }

    private void DeleteMerchant() {
        Merchants merchants = new Merchants();
        Scanner reader = new Scanner(System.in);
        merchantsAbstractDAO DML_delete = new merchantsDAO();

        System.out.println("请输入你要删除的ID");
        merchants.setId(reader.nextLong());

        int flag = DML_delete.delete(merchants);
        System.out.println(flag + "行受影响");
    }

    //@FIXME update没有传值
    private void UpdateMerchant() {
        Merchants merchants = new Merchants();
        Scanner reader = new Scanner(System.in);

        merchantsAbstractDAO DML_update = new merchantsDAO();
        int flag = DML_update.update(merchants);
        System.out.println(flag + "行受影响");

    }

    private void SearchMerchant(boolean b) throws IllegalAccessException {
        Merchants merchants = new Merchants();
        Scanner reader = new Scanner(System.in);
        merchantsAbstractDAO DQL = new merchantsDAO();
        if (b) { // b 为 ture 查询所有商家
            //格式化输出
            for (Merchants item : DQL.search(merchants)) {
                System.out.println(Toolset.table(Merchants.class, item));
            }
        } else {
            System.out.println("请输入你要查询的ID:");
            merchants = DQL.searchID(reader.nextLong());
            //格式化输出
            System.out.println(Toolset.table(Merchants.class, merchants));
        }
    }
}
