package com.liang.WT.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.WT.entity.BugManagementEntity;
import com.liang.WT.entity.ProjectEntity;
import com.liang.WT.service.BugManagementService;
import com.liang.WT.service.PrjProjectService;
import com.liang.common.base.BaseController;
import com.liang.common.base.BaseResult;
import com.liang.service.SysDictInfoService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Author ly
 * @Date 2025/4/2 9:44
 * @Version 1.0
 */

@Controller
@RequestMapping(value = "/managerment")
public class BugManagementController extends BaseController {

    @Resource
    private SysDictInfoService service;
    @Resource
    private PrjProjectService project;

    @Resource
    private BugManagementService bugManagementService;


    @RequestMapping(value = "/init",method = RequestMethod.GET)
    public String init(Model model){
        return "bugmanagement/list";
    }

    @ApiOperation(value ="页面初始化",notes = "页面初始化")
    @ResponseBody
    @RequestMapping(value = "/mylist",method = RequestMethod.POST)
    public BaseResult list(@RequestParam("page")Integer pageNum,
                           @RequestParam("limit")Integer pageSize,
                           @RequestParam(required = false,defaultValue = "",value = "name")String name,
                           @RequestParam(required = false,defaultValue = "",value = "code")String code,
                           @RequestParam(required = false,defaultValue = "",value = "status")String status){
        //分页
        PageHelper.startPage(pageNum,pageSize);
        //  创建 map 集合
        Map<String,Object>map = new HashedMap();
        map.put("name",name);
        map.put("code",code);
        map.put("status",status);
        List<BugManagementEntity> list = bugManagementService.mylist(map);
        PageInfo<BugManagementEntity> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<BugManagementEntity> 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(value = "/add",method = RequestMethod.GET)
    public String add(Model model){
        // 项目名称
        List<ProjectEntity>list = project.list(new HashMap<>());
        model.addAttribute("project",list);
        // 工作类型 work_type
        List<Map<String,String>> list1 = service.queryByCode("work_type");
        if (list1.size() > 0){
            model.addAttribute("workType",list1);
        }
        // 优先级 priority
        List<Map<String,String>> list2 = service.queryByCode("priority");
        if (list2.size() >0){
            model.addAttribute("priority",list2);
        }
        List<Map<String,String>> list3 = service.queryByCode("risk");
        if (list3.size() >0){
            model.addAttribute("risk",list3);
        }
        List<Map<String,String>> list4 = service.queryByCode("re_type");
        if (list4.size() >0){
            model.addAttribute("reType",list4);
        }
        List<Map<String,String>> list5 = service.queryByCode("demand");
        if (list5.size() >0){
            model.addAttribute("demand",list5);
        }
        // 风险等级 risk
        // 需求类型 re_type
        // 需求来源 demand
        return "bugmanagement/add";
    }
    @RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
    public String edit(Model model, @PathVariable Integer id){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        List<BugManagementEntity> list = bugManagementService.mylist(map);
            model.addAttribute("bugmanage",list.get(0));
        List<ProjectEntity>lists = project.list(new HashMap<>());
        model.addAttribute("project",lists);
        List<Map<String,String>> list1 = service.queryByCode("work_type");
        if (list1.size() > 0){
            model.addAttribute("workType",list1);
        }
        // 优先级 priority
        List<Map<String,String>> list2 = service.queryByCode("priority");
        if (list2.size() >0){
            model.addAttribute("priority",list2);
        }
        List<Map<String,String>> list3 = service.queryByCode("risk");
        if (list3.size() >0){
            model.addAttribute("risk",list3);
        }
        List<Map<String,String>> list4 = service.queryByCode("re_type");
        if (list4.size() >0){
            model.addAttribute("reType",list4);
        }
        List<Map<String,String>> list5 = service.queryByCode("demand");
        if (list5.size() >0){
            model.addAttribute("demand",list5);
        }
            return "bugmanagement/update";

    }
    @RequestMapping(value = "/view/{id}",method = RequestMethod.GET)
    public String view(Model model,
                       @PathVariable Integer id){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        List<BugManagementEntity> list = bugManagementService.mylist(map);
        model.addAttribute("bugmanage",list.get(0));
        return "bugmanagement/view";
    }


    @RequestMapping(value = "/init/{id}",method = RequestMethod.GET)
    public String init(Model model,
                       @PathVariable Integer id){

        Map<String,Integer> maps = new HashMap<>();
        maps.put("id",id);
        List<BugManagementEntity> list = bugManagementService.mylist(maps);
        if (list.size() == 1){
            model.addAttribute("bugmanage",list.get(0));
        }
        return "bugmanagement/init";
    }



    @RequestMapping(value = "adds",method = RequestMethod.POST)
    @ResponseBody
    public BaseResult adds(BugManagementEntity bugManagementEntity){
        bugManagementEntity.setCreateUser(getSysUserAccount());
        bugManagementEntity.setCreateUserId(getSysUserId());
        bugManagementEntity.setFlag(1);
        int num = bugManagementService.add(bugManagementEntity);
        if (num == 0){
            return BaseResult.failure(1,"保存失败！");
        }
        return BaseResult.success("数据保存成功！");
    }

    @RequestMapping(value = "updates",method = RequestMethod.POST)
    @ResponseBody
    public BaseResult updates(BugManagementEntity bugManagementEntity){
        bugManagementEntity.setUpdateUser(getSysUserAccount());
        bugManagementEntity.setUpdateUserId(getSysUserId());
        bugManagementEntity.setUpdateTime(new Date());
        int num = bugManagementService.update(bugManagementEntity);
        if (num == 0){
            return BaseResult.failure(1,"保存失败！");
        }
        return BaseResult.success("数据保存成功！");
    }

    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    @ResponseBody
    public BaseResult delete(Model model, @PathVariable Integer id){
        BugManagementEntity bugManagement = new BugManagementEntity();
        bugManagement.setId(id);
        bugManagement.setUpdateUser(getSysUserAccount());
        bugManagement.setUpdateUserId(getSysUserId());
        bugManagement.setUpdateTime(new Date());
        bugManagement.setFlag(0);
        int mun = bugManagementService.update(bugManagement);
        if (mun > 0){
            return BaseResult.success("删除成功");
        }
        return BaseResult.failure(1,"删除失败！");

    }
}
