package com.zehong.web.controller.operationMonitor;

import com.zehong.common.annotation.Log;
import com.zehong.common.core.controller.BaseController;
import com.zehong.common.core.domain.AjaxResult;
import com.zehong.common.core.domain.entity.SysUser;
import com.zehong.common.core.page.TableDataInfo;
import com.zehong.common.enums.BusinessType;
import com.zehong.common.utils.SecurityUtils;
import com.zehong.common.utils.poi.ExcelUtil;
import com.zehong.system.domain.SysPost;
import com.zehong.system.domain.THiddenTroubleInfo;
import com.zehong.system.domain.TWorkOrder;
import com.zehong.system.service.ISysPostService;
import com.zehong.system.service.ITHiddenTroubleInfoService;
import com.zehong.system.service.ITWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 燃气任务Controller
 * 
 * @author zehong
 * @date 2022-02-10
 */
@RestController
@RequestMapping("/system/order")
public class TWorkOrderController extends BaseController
{
    @Autowired
    private ITWorkOrderService tWorkOrderService;

    @Autowired
    private ISysPostService iSysPostService;

    @Autowired
    private ITHiddenTroubleInfoService troubleInfoService;

    /**
     * 查询燃气任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(TWorkOrder tWorkOrder)
    {
        startPage();
        judgeUserRole(tWorkOrder);
        List<TWorkOrder> list = tWorkOrderService.selectTWorkOrderList(tWorkOrder);
        return getDataTable(list);
    }

    /**
     * 导出燃气任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "燃气任务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TWorkOrder tWorkOrder)
    {
        judgeUserRole(tWorkOrder);
        List<TWorkOrder> list = tWorkOrderService.selectTWorkOrderList(tWorkOrder);
        ExcelUtil<TWorkOrder> util = new ExcelUtil<TWorkOrder>(TWorkOrder.class);
        return util.exportExcel(list, "燃气任务数据");
    }

    /**
     * 判断用户角色
     * @param tWorkOrder
     */
    private void judgeUserRole(TWorkOrder tWorkOrder){
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //获取用户所属单位
        long enterpriseId = user.getDeptId();
        //判断是否为政府用户
        if(!"-2".equals(String.valueOf(enterpriseId))){
            tWorkOrder.setWorkAssignEnterproseId(enterpriseId);
            //判断是否为巡线员
            List<Integer> postIds = iSysPostService.selectPostListByUserId(user.getUserId());
            for(Integer postId : postIds){
                SysPost postInfo = iSysPostService.selectPostById(postId.longValue());
                if("se".equals(postInfo.getPostCode()) && postIds.size()==1){
                    tWorkOrder.setWorkAssignManId(user.getUserId());
                }
            }
        }
    }

    /**
     * 获取燃气任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{workId}")
    public AjaxResult getInfo(@PathVariable("workId") Long workId)
    {
        return AjaxResult.success(tWorkOrderService.selectTWorkOrderById(workId));
    }

    /**
     * 新增燃气任务
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "燃气任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TWorkOrder tWorkOrder)
    {
        return toAjax(tWorkOrderService.insertTWorkOrder(tWorkOrder));
    }

    /**
     * 修改燃气任务
     */
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "燃气任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TWorkOrder tWorkOrder)
    {
        int edit = tWorkOrderService.updateTWorkOrder(tWorkOrder);
        if(1 == edit && null != tWorkOrder.getHiddenList() && !tWorkOrder.getHiddenList().isEmpty()){
            for(THiddenTroubleInfo tHiddenTroubleInfo : tWorkOrder.getHiddenList()){
                troubleInfoService.insertTHiddenTroubleInfo(tHiddenTroubleInfo);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 删除燃气任务
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "燃气任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workIds}")
    public AjaxResult remove(@PathVariable Long[] workIds)
    {
        return toAjax(tWorkOrderService.deleteTWorkOrderByIds(workIds));
    }

    @GetMapping("/selectWorkOrderNum")
    public AjaxResult selectWorkOrderNum(String enterpriseId)
    {
        return  AjaxResult.success(tWorkOrderService.selectWorkOrderNum(enterpriseId));
    }

}
