package com.huashe.park.application.web.controller.cons;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huashe.common.domain.AjaxResult;
import com.huashe.park.common.i18n.MessageUtils;
import com.huashe.park.core.service.IPileMachineInfoService;
import com.huashe.park.domain.dto.cons.MachineLogin;
import com.huashe.park.domain.entity.PileMachineInfo;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;

/**
 * 施工桩机信息Controller
 * 
 * @author ruoyi
 * @date 2025-02-12
 */
@RestController
@RequestMapping("/cons/pileMachineInfo")
public class PileMachineInfoController extends BaseController {
    @Autowired
    private IPileMachineInfoService pileMachineInfoService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询施工桩机信息列表
     */
    @PreAuthorize("@ss.hasPermi('cons:pileMachineInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(PileMachineInfo pileMachineInfo) {
        startPage();
        List<PileMachineInfo> list = pileMachineInfoService.selectPileMachineInfoList(pileMachineInfo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('cons:pileMachineInfo:list')")
    @GetMapping("/list/all")
    public TableDataInfo listAll(PileMachineInfo pileMachineInfo) {
        List<PileMachineInfo> list = pileMachineInfoService.selectPileMachineInfoList(pileMachineInfo);
        return getDataTable(list);
    }

    /**
     * 导出施工桩机信息列表
     */
    @PreAuthorize("@ss.hasPermi('cons:pileMachineInfo:export')")
    @Log(title = "施工桩机信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PileMachineInfo pileMachineInfo) {
        List<PileMachineInfo> list = pileMachineInfoService.selectPileMachineInfoList(pileMachineInfo);
        ExcelUtil<PileMachineInfo> util = new ExcelUtil<PileMachineInfo>(PileMachineInfo.class);
        util.exportExcel(response, list, "施工桩机信息数据");
    }

    /**
     * 获取施工桩机信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('cons:pileMachineInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(pileMachineInfoService.selectPileMachineInfoById(id));
    }

    /**
     * 新增施工桩机信息
     */
    @PreAuthorize("@ss.hasPermi('cons:pileMachineInfo:add')")
    @Log(title = "施工桩机信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PileMachineInfo pileMachineInfo) {
        return toAjax(pileMachineInfoService.insertPileMachineInfo(pileMachineInfo));
    }

    /**
     * 修改施工桩机信息
     */
    @PreAuthorize("@ss.hasPermi('cons:pileMachineInfo:edit')")
    @Log(title = "施工桩机信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PileMachineInfo pileMachineInfo) {
        return toAjax(pileMachineInfoService.updatePileMachineInfo(pileMachineInfo));
    }

    @PostMapping("/terminal/login")
    @Anonymous
    public AjaxResult login(@RequestBody MachineLogin machineAuth) {
        if (StringUtils.isEmpty(machineAuth.getSn()) || StringUtils.isEmpty(machineAuth.getNo())
            || StringUtils.isEmpty(machineAuth.getPwd())) {
            return AjaxResult.error(MessageUtils.message("machine.auth.param.error"));
        }

        List<PileMachineInfo> pileMachineInfos = pileMachineInfoService
            .selectPileMachineInfoList(new PileMachineInfo() {
                {
                    setSn(machineAuth.getSn());
                    setNo(machineAuth.getNo());
                }
            });
        if (CollectionUtils.isEmpty(pileMachineInfos)) {
            return AjaxResult.error(MessageUtils.message("machine.auth.param.none"));
        }
        PileMachineInfo pileMachineInfo = pileMachineInfos.get(0);
        if (!pileMachineInfo.getPwd().equals(machineAuth.getPwd())) {
            return AjaxResult.error(MessageUtils.message("machine.auth.param.none"));
        }
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
            pileMachineInfo.getSn(), pileMachineInfo.getPwd());
        authenticationToken.setDetails(pileMachineInfo);
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        String token = tokenService.createToken(machineAuth);
        pileMachineInfo.setToken(token);
        // 生成token
        return AjaxResult.success(pileMachineInfo);
    }

    /**
     * 删除施工桩机信息
     */
    @PreAuthorize("@ss.hasPermi('cons:pileMachineInfo:remove')")
    @Log(title = "施工桩机信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pileMachineInfoService.deletePileMachineInfoByIds(ids));
    }
}
