package com.liang.WT.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.WT.entity.ProgressEntity;
import com.liang.WT.entity.ProjectEntity;
import com.liang.WT.service.PrjProjectService;
import com.liang.WT.service.ProgressService;
import com.liang.common.base.BaseController;
import com.liang.common.base.BaseResult;
import io.swagger.annotations.ApiOperation;
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/3/25 10:26
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "/bug")
public class PrjProject extends BaseController {

    // 获取数据字典数值
    @Resource
    private PrjProjectService projectService;
    @Resource
    private ProgressService progressService;


    @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<ProjectEntity> list = projectService.list(map);
        PageInfo<ProjectEntity> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<ProjectEntity> 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);

    }
     /**
     * 新增数据
     * @param project
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public BaseResult add(ProjectEntity project){

        project.setCreateUser(getSysUserId());
        project.setCreateTime(new Date());
       int mun =  projectService.add(project);
       if (mun == 0){
           return BaseResult.failure(1,"保存失败");
       }
        return BaseResult.success("保存成功！");

    }

    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public BaseResult update(ProjectEntity project){
        project.setUpdateTime(new Date());
        project.setUpdateUser(getSysUserId());
        int mun = projectService.update(project);
        if (mun == 0){
            return BaseResult.failure(1,"数据返回异常，修改失败！");
        }
        return  BaseResult.success("修改成功！");
    }

    /**
     * 删除接口 （逻辑删除）
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/de/{id}",method = RequestMethod.POST)
    public BaseResult view(@PathVariable Integer id){
       ProjectEntity pr =  new ProjectEntity();
       pr.setId(id);
       pr.setFlag(1);
        int mun = projectService.update(pr);
        if (mun == 1){
            return  BaseResult.success("删除成功！");
        }

        return BaseResult.failure(1,"删除失败！");
    }


    @ResponseBody
    @RequestMapping(value = "/progressupdate/{type}",method = RequestMethod.POST)
    public BaseResult progressupdate(ProgressEntity progress,@PathVariable String type){

        progress.setCreateTime(new Date());
        progress.setCreateUser(getSysUserAccount());
        progress.setFlag(0);
        progress.setType(type);
        int mun = progressService.add(progress);
        if (mun >0){
            return BaseResult.success("发布成功！");
        }
        return BaseResult.failure(1,"发布失败");
    }

    @ResponseBody
    @RequestMapping(value = "/progresslist/{xmid}",method = RequestMethod.POST)
    public BaseResult progresslist(@RequestParam("page")Integer pageNum,
                           @RequestParam("limit")Integer pageSize,
                           @PathVariable Integer xmid,
                                   @RequestParam("type") String type){
        //分页
        PageHelper.startPage(pageNum,pageSize);
        //  创建 map 集合
        Map<String,Object>map = new HashedMap();
        map.put("xmid",xmid);
        map.put("type",type);
        List<ProgressEntity> list = progressService.getAll(map);
        PageInfo<ProgressEntity> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<ProgressEntity> 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);

    }


    @ResponseBody
    @RequestMapping(value = "/prodelete/{id}",method = RequestMethod.POST)
    public BaseResult progressdelete(@PathVariable Integer id){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("cratetime",new Date());
        map.put("flag",1);
        Integer mun = progressService.progressDelete(map);
        if (mun >0){
            return BaseResult.success("删除成功！");
        }
        return BaseResult.failure(1,"删除失败！");
    }

}
