package cn.palmte.work.controller.backend;

import cn.palmte.work.bean.ResponseMsg;
import cn.palmte.work.model.ProcurementType;
import cn.palmte.work.service.ProcurementTypeService;
import cn.palmte.work.utils.Utils;
import cn.palmte.work.utils.excel.ExportUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/procurement/type")
public class ProcurementTypeController extends BaseController{

    @Autowired
    private ProcurementTypeService procurementTypeService;

    /**
     * 列表查询
     * @param keywords
     * @param pageNumber
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value = "keywords",required = false) String keywords,
                       @RequestParam(value = PAGE_NUMBER, defaultValue = DEFAULT_PAGE_NUMBER) int pageNumber,
                       @RequestParam(value = PAGE_SIZE, defaultValue = DEFAULT_PAGE_SIZE) int pageSize,
                       Map<String, Object> model) {
        model.put("keywords",keywords);
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo(keywords,model);
        model.put(PAGER,procurementTypeService.list(searchInfo,pageNumber,pageSize));
        return "/admin/procurement_type_list";
    }

    /**
     * 跳转新增页面
     * @param model
     * @return
     */
    @GetMapping(value = "/add")
    public String add( Map<String, Object> model){
        ProcurementType procurementType = new ProcurementType();
        model.put("procurementTypeId",-1);
        model.put("procurementType",procurementType);
        return "/admin/procurement_type_input";
    }

    /**
     * 跳转到编辑页面
     */
    @RequestMapping("/edit")
    public String edit(@RequestParam("id") int id, Map<String, Object> model) {
        ProcurementType procurementType = procurementTypeService.findOne(id);
        model.put("procurementTypeId",id);
        model.put("procurementType", procurementType);
        return "/admin/procurement_type_input";
    }

    /**
     * 保存
     * @param procurementTypeId
     * @param procurementType
     * @param model
     * @return
     */
    @PostMapping(value = "/save")
    public String save(@RequestParam("procurementTypeId") int procurementTypeId,
                       ProcurementType procurementType, Map<String, Object> model){

        procurementTypeService.saveOtUpdate(procurementTypeId,procurementType);
        return "redirect:/procurement/type/list";
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @GetMapping(value = "/delete")
    @ResponseBody
    public ResponseMsg delete(@RequestParam("ids") String ids){
        String[] deleteIds=ids.split("#%#");
        boolean deleted = procurementTypeService.deleteByIDs(deleteIds);

        ResponseMsg responseMsge=new ResponseMsg();
        if(deleted){
            responseMsge.setStatus(0);
            responseMsge.setMsg("删除成功");
        } else{
            responseMsge.setStatus(1);
            responseMsge.setMsg("删除失败");
        }
        return responseMsge;
    }

    /**
     * 启用 禁用
     */
    @RequestMapping("/enableOrDisable")
    @ResponseBody
    public ResponseMsg enableOrDisable(@RequestParam("id") int id,
                                       @RequestParam("status") int status, RedirectAttributes attr) {
        boolean isSuccess = false;
        try {
            isSuccess = procurementTypeService.enableOrDisable(status, id);
        } catch (Exception e) {
            e.getMessage();
        }
        if (isSuccess) {
            return ResponseMsg.buildSuccessMsg("操作成功");
        } else {
            return ResponseMsg.buildSuccessMsg("操作失败");
        }
    }

    /**
     * 导出
     */
    @RequestMapping("/export")
    public void export(@RequestParam(value = "keywords",required = false) String keywords, HttpServletResponse httpServletResponse) throws IOException {
        Map<String, String> searchInfo = getSearchInfo(keywords);
        downloadHeader(httpServletResponse , Utils.generateExcelName("采购类型表"), "application/octet-stream");
        String[] headers = {"采购类型名称","所属大类","创建人","创建时间"};
        String[] exportColumns = {"name","typeName","createdBy","createdTime"};
        ExportUtils.exportToExcel(headers, exportColumns, 1, 10000,
                httpServletResponse.getOutputStream(), (pN, pS) -> procurementTypeService.list(searchInfo, pN, pS).getList());
    }

    /**
     * 类型和类别的映射关系
     */
    @RequestMapping("/map")
    @ResponseBody
    public ResponseMsg map() {
        List<ProcurementType> procurementTypes = procurementTypeService.allProcurementTypeList();
        Map<Integer, List<ProcurementType>> map = procurementTypes.stream().collect(Collectors.groupingBy(ProcurementType::getType));

        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("procurementTypes", procurementTypes);
        objectMap.put("map", map);
        return ResponseMsg.buildSuccessData(objectMap);
    }

}
