package com.bonus.hn.bearing.capacity.backstage.project.controller;

import com.bonus.hn.bearing.capacity.backstage.manager.annotation.LogAnnotation;
import com.bonus.hn.bearing.capacity.backstage.manager.table.PageTableRequest;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.AjaxRes;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.ExcelUtils;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.GlobalConst;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.R;
import com.bonus.hn.bearing.capacity.backstage.project.entity.FileBean;
import com.bonus.hn.bearing.capacity.backstage.project.entity.LineProjectBean;
import com.bonus.hn.bearing.capacity.backstage.project.entity.RnProjectBindBean;
import com.bonus.hn.bearing.capacity.backstage.project.service.RnProjectBindService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xliu
 * @date 2023/11/6 14:45  杆塔工程管理
 */
@RestController
@RequestMapping("/RnProjectBind")
public class RnProjectBindController {

    @Resource
    private RnProjectBindService service;

    @LogAnnotation
    @PostMapping("getRnProjectList")
    @ApiOperation(value = "实名制工程绑定-列表")
    public R getRnProjectList(RnProjectBindBean o){
        List<RnProjectBindBean> list = service.getRnProjectList(o);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }

    @LogAnnotation
    @PostMapping("delRnProjectById")
    @PreAuthorize("hasAuthority('sysRnProject:untie')")
    @ApiOperation(value = "实名制工程绑定-解绑")
    public AjaxRes delRnProjectById(FileBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.delRnProjectById(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @LogAnnotation
    @PostMapping("updateRnProject")
    @PreAuthorize("hasAuthority('sysRnProject:bind')")
    @ApiOperation(value = "实名制工程绑定-绑定")
    public AjaxRes updateRnProject(@RequestBody RnProjectBindBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.updateRnProject(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    /**
     * 实名制工程绑定导出
     * @param request
     * @param response
     * @param page
     */
    @RequestMapping(value = "/exportPhysicalCheckResult", method = RequestMethod.GET)
    @ApiOperation(value = "实名制工程绑定-导出")
    public void exportPayroll(HttpServletRequest request, HttpServletResponse response, PageTableRequest page) throws ParseException {
        RnProjectBindBean b = new RnProjectBindBean();
        b.setProName(request.getParameter("proName"));
        b.setRealName(request.getParameter("realName"));
        b.setStatusId(request.getParameter("statusId"));
        List<RnProjectBindBean> list = service.getExportList(b);
        List<Map<String, Object>> mapList = new ArrayList<>();
        String[] excelHeader = {"序号","承载力工程名称","实名制线路工程名称","匹配状态"};
        int[] ds_format = {2, 2, 2, 2};
        String fileName = "实名制工程绑定清单";
        if (list != null) {
            int i = 0;
            for (RnProjectBindBean bean : list){
                i++;
                Map<String, Object> map = new HashMap<>(100);
                map.put("序号",i);
                map.put("承载力工程名称",bean.getProName());
                map.put("实名制线路工程名称",bean.getRealName());
                String proStatus = "0".equals(bean.getMach()) ? "未匹配" : "已匹配";
                map.put("匹配状态", proStatus);
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
