package com.liang.controller;


import com.github.pagehelper.PageInfo;
import com.liang.common.base.BaseResult;
import com.liang.common.base.BaseController;
import com.liang.entity.CyyEntriy;
import com.liang.entity.SysUserInfo;
import com.liang.service.CyyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("prjProject")
public class CyyController extends BaseController {


    @Autowired
    private CyyService cyyService;

    /**
     * 初始化列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/cyyList", method = RequestMethod.POST)
    public BaseResult cyyList(Model model, CyyEntriy cyyEntriy) {
        SysUserInfo userInfo = getSysUserInfo();
        model.addAttribute("userInfo", userInfo);
        List<CyyEntriy> list = this.cyyService.queryByAccount(String.valueOf(userInfo.getUserId()));
        PageInfo<CyyEntriy> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<CyyEntriy> rows = pageinfo.getList();
        int total = (int) pageinfo.getTotal();
        Map<String, Object> result = new HashMap<>();
        result.put(RESULT_ROWS, rows);
        result.put(RESULT_TOTAL, total);

        return BaseResult.success(result);
    }

    //常用语
    @RequestMapping("/forCyyy")
    public String forCyyy() {
        return "prjproject/cyy";
    }

    //查询
    @RequestMapping("/forCyyView")
    public String forCyyView(Model model) {
        SysUserInfo userInfo = getSysUserInfo();
        model.addAttribute("userInfo", userInfo);
        return "prjproject/cyyview";
    }

    // 添加常用语
    @RequestMapping("/forCyyAdd")
    public String forCyyAdd(Model model) {
        SysUserInfo userInfo = getSysUserInfo();
        model.addAttribute("userInfo", userInfo);
        return "prjproject/cyyadd";
    }

    @RequestMapping("/doCyyAdd")
    public ResponseEntity<CyyEntriy> doCyyAdd(CyyEntriy cyyEntriy) {
        CyyEntriy cyyEntriy1 = new CyyEntriy();
        cyyEntriy1.setId(cyyEntriy.getId());
        cyyEntriy1.setCyy(cyyEntriy.getCyy());
        cyyEntriy1.setAccount(cyyEntriy.getAccount());
        cyyEntriy1.setYhm(cyyEntriy.getYhm());
        return ResponseEntity.ok(this.cyyService.insert(cyyEntriy1));
    }

    // 修改常用语
    @RequestMapping("/forUpdate")
    public String forUpdate(Model model) {
        SysUserInfo userInfo = getSysUserInfo();
        model.addAttribute("userInfo", userInfo);
        return "prjproject/cyy_update";
    }

    @RequestMapping("/doUpdate")
    public ResponseEntity<CyyEntriy> doUpdate(CyyEntriy cyyEntriy) {
        CyyEntriy cyyEntriy1 = new CyyEntriy();
        cyyEntriy1.setId(cyyEntriy.getId());
        cyyEntriy1.setCyy(cyyEntriy.getCyy());
        cyyEntriy1.setAccount(cyyEntriy.getAccount());
        cyyEntriy1.setYhm(cyyEntriy.getYhm());
        return ResponseEntity.ok(this.cyyService.update(cyyEntriy1));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping(value = "forCyyDelete/{id}", method = RequestMethod.GET)
        public ResponseEntity<Boolean> deleteById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.cyyService.deleteById(id));
    }

}
