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.utils.AjaxRes;
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.LineProjectBean;
import com.bonus.hn.bearing.capacity.backstage.project.entity.TowerProjectBean;
import com.bonus.hn.bearing.capacity.backstage.project.service.LineProjectService;
import com.bonus.hn.bearing.capacity.backstage.project.service.TowerProjectService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

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

    @Resource
    private TowerProjectService service;

    @LogAnnotation
    @PostMapping("getFirmContentList")
    @ApiOperation(value = "线路工程-列表")
    public R getFirmList(TowerProjectBean o){
        List<TowerProjectBean> list = service.getFirmList(o);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }

    @RequestMapping(value = "addProjectInfo", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('sysTowerProject:add')")
    @ApiOperation(value = "工程管理-杆塔管理-新增")
    public AjaxRes addProjectInfo(@RequestBody TowerProjectBean bean) throws Exception {
        AjaxRes ar = new AjaxRes();
        int cb = service.addProjectInfo(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
        }
        return  ar;
    }

    @RequestMapping(value = "getListProject", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "工程管理-杆塔管理-查询人员信息")
    public AjaxRes getListProject(String id){
        AjaxRes ar = new AjaxRes();
        TowerProjectBean towerProjectBean = service.getListProject(id);
        Map<String, Object> map = new HashMap<>(100);
        if(towerProjectBean !=null){
            map.put("towerProjectBean",towerProjectBean);
        }
        ar.setSucceed(map,GlobalConst.DATA_SUCCEED);
        return ar;
    }

    @RequestMapping(value = "updateProject", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('sysTowerProject:update')")
    @ApiOperation(value = "工程管理-杆塔管理-修改")
    public AjaxRes updateProject(@RequestBody TowerProjectBean bean) throws Exception {
        AjaxRes ar = new AjaxRes();
        int cb = service.updateProject(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
        }
        return  ar;
    }

    @RequestMapping(value = "delProjectByIds", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('sysTowerProject:batchDel')")
    @ApiOperation(value = "工程管理-杆塔管理-批量删除")
    public AjaxRes delProjectByIds(@RequestParam("ids") String ids) throws Exception {
        AjaxRes ar = new AjaxRes();
        if (StringUtils.isBlank(ids)) {
            ar.setFailMsg("请选择要删除的数据");
        } else {
            // 将逗号分隔的字符串拆分成数组
            String[] idArray = ids.split(",");
            int successCount = 0;
            for (String id : idArray) {
                int result = service.delProjectById(id);
                if (result > 0) {
                    successCount++;
                }
            }
            if (successCount == idArray.length) {
                ar.setSucceedMsg("批量删除成功");
            } else {
                ar.setFailMsg("批量删除失败");
            }
        }
        return ar;
    }

    @RequestMapping(value = "delProjectId", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('sysTowerProject:del')")
    @ApiOperation(value = "工程管理-杆塔管理-删除")
    public AjaxRes delProjectId(String id) throws Exception {
        AjaxRes ar = new AjaxRes();
        int cb = service.delProjectId(id);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
        }
        return  ar;
    }

    /**
     * 文件上传请求
     */
    @LogAnnotation
    @PostMapping("/towerListImport")
    @ApiOperation(value = "杆塔管理-文件导入")
    public AjaxRes toolListImport(@RequestParam("file") MultipartFile file, @RequestParam("engineerId") String engineerId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        AjaxRes ar = new AjaxRes();
        try {
            String code = service.towerListImport(request,file,engineerId);
            if ("导入成功".equals(code)){
                ar.setSucceedMsg("导入成功");
            }else{
                ar.setFailMsg(code);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ar;
    }


}
