package com.liang.controller;

import com.github.pagehelper.PageHelper;
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.*;
import com.liang.service.PrjMemberService;
import com.liang.service.PrjProjectdivisionService;
import com.liang.service.PrjProjectroleService;
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;

/**
 * 项目人员(PrjMember)表控制层
 *
 * @author makejava
 * @since 2023-03-10 11:21:54
 */
@Controller
@RequestMapping("prjMember")
public class PrjMemberController extends BaseController {

    /**
     * 服务对象
     */
    @Resource
    private PrjMemberService prjMemberService;
    @Resource
    private PrjProjectroleService prjProjectroleService;
    @Resource
    private PrjProjectdivisionService prjProjectdivisionService;

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

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<PrjMember> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.prjMemberService.queryById(id));
    }

    /**
     * 编辑数据
     *
     * @param prjMember 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<PrjMember> edit(PrjMember prjMember) {
        return ResponseEntity.ok(this.prjMemberService.update(prjMember));
    }

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

    /**
     * 主要完成人
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{xm}", method = RequestMethod.POST)
    public BaseResult list(@PathVariable Integer xm) {
        List<PrjMember> list = prjMemberService.getMemberInfoList(xm);

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

    /**
     * 主要完成人
     *
     * @return
     */
    @ApiOperation(value = "页面初始化", notes = "页面初始化")
    @RequestMapping(value = "/forAdd", method = RequestMethod.GET)
    public String forAdd(Model model) {
        //项目角色
        List<Map<String, String>> xmjsList = prjProjectroleService.getProjectRole();
        model.addAttribute("xmjsList", xmjsList);

        //本项目分工
        List<Map<String, String>> bxmfgList = prjProjectdivisionService.getPrjDivision();
        model.addAttribute("bxmfgList", bxmfgList);

        return "prjmember/add";
    }

    /**
     * 新建项目人员
     *
     * @param prjMember
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    public BaseResult doAdd(PrjMember prjMember) {

        int num = prjMemberService.insert(prjMember);
        if (num > 0) {
            //本项目分工
            prjProjectdivisionService.insert(prjMember.getBxmfg());
            return BaseResult.success("项目人员保存成功！");
        } else if (num == -1) {
            return BaseResult.success("该项目人员已存在，如需修改，请选中后编辑！");
        } else {
            return BaseResult.success("项目人员保存失败！");
        }
    }

    /**
     * 批量保存项目人员
     *
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchInsert/{xm}", method = RequestMethod.POST)
    public BaseResult batchInsert(@RequestBody String data, @PathVariable Integer xm) {
        //删除项目人员
        prjMemberService.deleteByXm(xm);

        List<PrjMember> memberList = JsonTool.parseArray(data, PrjMember.class);
        if (memberList.size() > 0) {
            int num = prjMemberService.insertBatch(memberList);
            if (num == memberList.size()) {
                return BaseResult.success("项目成员保存成功！");
            } else {
                return BaseResult.failure("项目成员保存失败！");
            }
        } else {
            return BaseResult.success("没有要保存的信息");
        }
    }

    @ApiOperation(value = "修改页面初始化", notes = "修改页面初始化")
    @RequestMapping(value = "/forUpdate", method = RequestMethod.GET)
    public String forUpdate(Model model) {
        //项目角色
        List<Map<String, String>> xmjsList = prjProjectroleService.getProjectRole();
        model.addAttribute("xmjsList", xmjsList);
        //本项目分工
        List<Map<String, String>> bxmfgList = prjProjectdivisionService.getPrjDivision();
        model.addAttribute("bxmfgList", bxmfgList);

        return "prjmember/update";
    }

    @ApiOperation(value = "浏览页面初始化", notes = "浏览页面初始化")
    @RequestMapping(value = "/forView", method = RequestMethod.GET)
    public String forView(Model model) {
        return "prjmember/view";
    }

    /**
     * 项目人员列表
     *
     * @param pageNum
     * @param pageSize
     * @param xm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/rylist/{xm}", method = RequestMethod.GET)
    public Object rylist(@PathVariable Integer xm,
                         @RequestParam("page") Integer pageNum,
                         @RequestParam("limit") Integer pageSize,
                         @RequestParam(required = false, defaultValue = "", value = "ryxm") String ryxm) {
        PageHelper.startPage(pageNum, pageSize);

        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("xm", xm.toString());
        paraMap.put("ryxm", ryxm);
        List<PrjMember> list = prjMemberService.getMemberList(paraMap);

        PageInfo<PrjMember> pageinfo = new PageInfo<>(list);
        // 取出查询结果
        int total = (int) pageinfo.getTotal();

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        resultMap.put("count", total);
        resultMap.put("data", list);

        return resultMap;
    }
}

