package com.jsc.oscm.controller;

import com.alibaba.fastjson.JSON;
import com.jsc.oscm.entity.WordBank;
import com.jsc.oscm.model.ReturnT;
import com.jsc.oscm.service.WordBankService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class WordBankController {

    @Resource
    WordBankService wordBankService;

    @PostMapping("/wordBank/add")
    public String add(@RequestBody WordBank wordBank) {
        ReturnT<WordBank> returnT;
        try {
            wordBank.setDelFlag(0);
            wordBank.setCreateTime(new Date());
            wordBank.setUpdateTime(new Date());
            returnT = new ReturnT<>(200, "添加成功", wordBankService.save(wordBank));
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/wordBank/update")
    public String update(@RequestBody WordBank wordBank) {
        ReturnT<WordBank> returnT;
        try {
            returnT = new ReturnT<>(200, "修改成功", wordBankService.save(wordBank));
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/wordBank/deleteByIds")
    public String deleteByIds(@RequestBody List<String> ids) {
        ReturnT<String> returnT;
        try {
            wordBankService.delete(ids);
            returnT = new ReturnT<>(200, "批量删除成功", null);
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/wordBank/delete")
    public String delete(@RequestBody String id) {
        ReturnT<String> returnT;
        try {
            wordBankService.delete(Integer.parseInt(id));
            returnT = new ReturnT<>(200, "删除成功", null);
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/wordBank/queryPageable/{pageIndex}/{pageSize}")
    public String query(@RequestBody WordBank wordBank,
                        @PathVariable("pageIndex") Integer pageIndex,
                        @PathVariable("pageSize") Integer pageSize) {
        ReturnT<List<WordBank>> returnT;
        try {
            returnT = new ReturnT<>(200,
                    wordBankService.queryCount(wordBank).toString(),
                    wordBankService.queryPageable(wordBank, pageIndex, pageSize));
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/open/wordBank/queryAll")
    public String queryAll(@RequestBody Map<String, Object> params) {
        ReturnT<List<WordBank>> returnT;
        try {
            List<WordBank> resultList = wordBankService.queryAll(params);
            returnT = new ReturnT<>(200, String.valueOf(resultList.size()), resultList);
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

}
