package com.liang.controller;

import com.github.pagehelper.PageInfo;
import com.liang.common.JsonTool;
import com.liang.common.base.BaseController;
import com.liang.common.base.BaseResult;
import com.liang.entity.PrjContacts;
import com.liang.service.PrjContactsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

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

/**
 * (PrjContacts)表控制层
 *
 * @author makejava
 * @since 2023-04-11 15:04:32
 */
@Controller
@RequestMapping("prjContacts")
public class PrjContactsController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private PrjContactsService prjContactsService;

    /**
     * 分页查询
     *
     * @param prjContacts 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<PrjContacts>> queryByPage(PrjContacts prjContacts, PageRequest pageRequest) {
        return ResponseEntity.ok(this.prjContactsService.queryByPage(prjContacts, pageRequest));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.prjContactsService.deleteById(id));
    }

    /**
     * 联系方式列表
     *
     * @param xm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{xm}", method = RequestMethod.POST)
    public BaseResult list(@PathVariable Integer xm) {
        List<PrjContacts> list = prjContactsService.getContactList(xm);

        PageInfo<PrjContacts> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<PrjContacts> 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);
    }

    @ApiOperation(value = "页面初始化", notes = "页面初始化")
    @RequestMapping(value = "/forAdd/{xm}", method = RequestMethod.GET)
    public String forAdd(Model model, @PathVariable Integer xm) {
        model.addAttribute("xm", xm);
        return "prjcontacts/add";
    }

    /**
     * 新增数据
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    public BaseResult doAdd(PrjContacts prjContacts) {

        int num = prjContactsService.insert(prjContacts);
        if (num > 0) {
            return BaseResult.success("保存成功！");
        } else if (num == -1) {
            return BaseResult.success("该单位联系人已存在，如需修改，请选中后编辑！");
        } else {
            return BaseResult.success("保存失败！");
        }
    }

    @RequestMapping(value = "/forUpdate/{id}", method = RequestMethod.GET)
    public String forUpdat(Model model, @PathVariable Integer id) {
        PrjContacts prjContacts = prjContactsService.queryById(id);
        model.addAttribute("prjContacts", prjContacts);
        return "prjcontacts/update";
    }

    @RequestMapping(value = "/doUpdate", method = RequestMethod.POST)
    public BaseResult doUpdate(PrjContacts prjContacts) {

        int num = prjContactsService.update(prjContacts);
        if (num > 0) {
            return BaseResult.success("保存成功！");
        } else {
            return BaseResult.success("保存失败！");
        }
    }

    @RequestMapping(value = "/forView/{id}", method = RequestMethod.GET)
    public String forView(Model model, @PathVariable Integer id) {
        PrjContacts prjContacts = prjContactsService.queryById(id);
        model.addAttribute("prjContacts", prjContacts);
        return "prjcontacts/view";
    }

    @ResponseBody
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    public BaseResult batchDelete(@RequestBody String data) {

        List<Integer> idList = JsonTool.getListID(data);
        int num = prjContactsService.batchDelete(idList);
        if (num > 0) {
            return BaseResult.success("批量删除成功!");
        } else {
            return BaseResult.failure("批量删除失败!");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public BaseResult delete(@PathVariable Integer id) {
        boolean flag = prjContactsService.deleteById(id);
        if (flag) {
            return BaseResult.success("删除成功!");
        } else {
            return BaseResult.failure("删除失败!");
        }
    }
}

