package com.zehong.web.controller.task;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.zehong.common.utils.StringUtils;
import com.zehong.system.domain.TEnterpriseInfo;
import com.zehong.system.domain.TWorkTaskPatrol;
import com.zehong.system.service.ITEnterpriseInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zehong.common.annotation.Log;
import com.zehong.common.core.controller.BaseController;
import com.zehong.common.core.domain.AjaxResult;
import com.zehong.common.enums.BusinessType;
import com.zehong.system.domain.TLinePatrol;
import com.zehong.system.service.ITLinePatrolService;
import com.zehong.common.utils.poi.ExcelUtil;
import com.zehong.common.core.page.TableDataInfo;

/**
 * 巡线Controller
 *
 * @author zehong
 * @date 2026-02-02
 */
@RestController
@RequestMapping("/line/patrol")
public class TLinePatrolController extends BaseController
{
    @Autowired
    private ITLinePatrolService tLinePatrolService;

    @Autowired
    private ITEnterpriseInfoService tEnterpriseInfoService;

    /**
     * 查询巡线列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TLinePatrol tLinePatrol)
    {
        startPage();
        List<TLinePatrol> list = tLinePatrolService.selectTLinePatrolList(tLinePatrol);
        return getDataTable(list);
    }

    /**
     * 导出巡线列表
     */
    @Log(title = "巡线", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TLinePatrol tLinePatrol)
    {
        List<TLinePatrol> list = tLinePatrolService.selectTLinePatrolList(tLinePatrol);
        ExcelUtil<TLinePatrol> util = new ExcelUtil<TLinePatrol>(TLinePatrol.class);
        return util.exportExcel(list, "巡线数据");
    }

    /**
     * 获取巡线详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tLinePatrolService.selectTLinePatrolById(id));
    }

    /**
     * 新增巡线
     */
    @Log(title = "巡线", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TLinePatrol tLinePatrol)
    {
        return toAjax(tLinePatrolService.insertTLinePatrol(tLinePatrol));
    }

    /**
     * 修改巡线
     */
    @Log(title = "巡线", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TLinePatrol tLinePatrol)
    {
        return toAjax(tLinePatrolService.updateTLinePatrol(tLinePatrol));
    }

    /**
     * 删除巡线
     */
    @Log(title = "巡线", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tLinePatrolService.deleteTLinePatrolByIds(ids));
    }

    @GetMapping("/getPatrolChars/{year}")
    public AjaxResult getPatrolChars(@PathVariable("year") String year)
    {
        if("".equals(year) || year==null){
            Calendar calendar = Calendar.getInstance();
            year = String.valueOf(calendar.get(Calendar.YEAR)) + '-';
        }
        List<TLinePatrol> list = tLinePatrolService.selectTLinePatrolList(new TLinePatrol());
        String finalYear = year;
        list.removeIf(e->!e.getLinePatrolTime().contains(finalYear));
        Map<String, List<TLinePatrol>> collect = list.stream().collect(Collectors.groupingBy(e -> e.getBeyondEnterpriseName()));

        List<TEnterpriseInfo> tEnterpriseInfoList = tEnterpriseInfoService.selectTEnterpriseInfoList(new TEnterpriseInfo());
        String[] enterpriseName = tEnterpriseInfoList.stream()
                .map(TEnterpriseInfo::getEnterpriseName)
                .toArray(String[]::new);
        int[] numbers = new int[enterpriseName.length];
        for (int i = 0; i < enterpriseName.length; i++) {
            List<TLinePatrol> tLinePatrols = collect.get(enterpriseName[i]);
            Integer num = tLinePatrols==null? 0 : tLinePatrols.size();
            numbers[i] = num;
        }
        HashMap resultMap = new HashMap();
        resultMap.put("enterpriseName",enterpriseName);
        resultMap.put("numbers",numbers);
        return AjaxResult.success(resultMap);
    }
}