package com.zhyc.module.frozen.controller;

import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.zhyc.module.frozen.domain.DdFe;
import com.zhyc.module.frozen.domain.DdFs;
import com.zhyc.module.frozen.service.IDdFeService;
import com.zhyc.module.frozen.service.IDdFsService;
import org.springframework.security.access.prepost.PreAuthorize;
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.zhyc.common.annotation.Log;
import com.zhyc.common.core.controller.BaseController;
import com.zhyc.common.core.domain.AjaxResult;
import com.zhyc.common.enums.BusinessType;
import com.zhyc.module.frozen.domain.DdSale;
import com.zhyc.module.frozen.service.IDdSaleService;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;

/**
 * 销售主单Controller
 *
 * @author HashMap
 * @date 2025-12-01
 */
@RestController
@RequestMapping("/sale/sale")
public class DdSaleController extends BaseController {
    private final IDdSaleService ddSaleService;
    private final IDdFeService ddFeService;
    private final IDdFsService ddFsService;

    public DdSaleController(IDdSaleService ddSaleService, IDdFeService ddFeService, IDdFsService ddFsService) {
        this.ddSaleService = ddSaleService;
        this.ddFeService = ddFeService;
        this.ddFsService = ddFsService;
    }

    /**
     * 查询销售主单列表
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:list')")
    @GetMapping("/list")
    public TableDataInfo list(DdSale ddSale) {
        startPage();
        List<DdSale> list = ddSaleService.selectDdSaleList(ddSale);
        return getDataTable(list);
    }

    /**
     * 导出销售主单列表
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:export')")
    @Log(title = "销售主单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DdSale ddSale) {
        List<DdSale> list = ddSaleService.selectDdSaleList(ddSale);
        ExcelUtil<DdSale> util = new ExcelUtil<>(DdSale.class);
        util.exportExcel(response, list, "销售主单数据");
    }

    /**
     * 获取销售主单详细信息
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ddSaleService.selectDdSaleById(id));
    }

    /**
     * 新增销售主单
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:add')")
    @Log(title = "销售主单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DdSale ddSale) {
        return toAjax(ddSaleService.insertDdSale(ddSale));
    }

    /**
     * 修改销售主单
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:edit')")
    @Log(title = "销售主单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DdSale ddSale) {
        return toAjax(ddSaleService.updateDdSale(ddSale));
    }

    /**
     * 删除销售主单
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:remove')")
    @Log(title = "销售主单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ddSaleService.deleteDdSaleByIds(ids));
    }

    @GetMapping("/codeDict")
    public AjaxResult getSaleItemCodeDict() {
        DdFe fe = new DdFe();
        fe.setStatus("正常");
        DdFs fs = new DdFs();
        fs.setStat("正常");
        List<DdFe> ddFeList = Optional
                .ofNullable(ddFeService.selectDdFeList(fe))
                .orElse(Collections.emptyList());

        List<DdFs> ddFsList = Optional
                .ofNullable(ddFsService.selectDdFsList(fs))
                .orElse(Collections.emptyList());

        // 提取 code 字段
        List<String> feCodeList = ddFeList.stream()
                .map(DdFe::getCode)
                .collect(Collectors.toList());

        List<String> fsCodeList = ddFsList.stream()
                .map(DdFs::getCode)
                .collect(Collectors.toList());

        Map<String, List<String>> saleItemCodeDict = new HashMap<>(2);
        saleItemCodeDict.put("embryo", feCodeList);
        saleItemCodeDict.put("semen", fsCodeList);

        return AjaxResult.success(saleItemCodeDict);
    }

}
