package com.ruoyi.project.business.controller.quotation;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.ruoyi.common.enums.FileBuzType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.annotation.DataUpdateCheck;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.PageDomain;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.domain.TbFile;
import com.ruoyi.project.business.domain.TqQuotation;
import com.ruoyi.project.business.domain.bo.quotation.QuotationAddBO;
import com.ruoyi.project.business.domain.bo.quotation.QuotationCopyBO;
import com.ruoyi.project.business.domain.bo.quotation.QuotationQueryBO;
import com.ruoyi.project.business.domain.bo.quotation.*;
import com.ruoyi.project.business.domain.vo.quotation.QuotationVO;
import com.ruoyi.project.business.service.ITbFileService;
import com.ruoyi.project.business.service.quotation.ITqQuotationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2024-05-10
 */
@RestController
@RequestMapping("/business/quotation")
@Api("报价")
public class TqQuotationController extends BaseController {

    @Resource
    private ITqQuotationService tqQuotationService;
    @Resource
    private ITbFileService tbFileService;

    /**
     * 查询【报价】列表
     */
    // @PreAuthorize("@ss.hasPermi('business:quotation:list')")
    @GetMapping("/list")
    public TableDataInfo list(QuotationQueryBO queryBO) {
        startPage();
        List<QuotationVO> list = tqQuotationService.selectTqQuotationList(queryBO);
        return getDataTable(list);
    }

    // 手机端 - 议价审批列表
    @GetMapping("/approval/list")
    @ApiOperation("议价审批列表")
    public TableDataInfo listApproval(PageDomain pageDomain) {
        startPage();
        return getDataTable(tqQuotationService.listApproval());
    }

    // 手机端 - 议价审批详情页
    @GetMapping("/approval/detail/{id}")
    @ApiOperation("议价审批详情页")
    public AjaxResult detailApproval(@PathVariable("id") Long id) {
        return success(tqQuotationService.detailApproval(id));
    }

    /**
     * 获取【报价详情】详细信息
     */
    // @PreAuthorize("@ss.hasPermi('business:quotation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tqQuotationService.selectTqQuotationById(id));
    }

    /**
     * 新增【新增报价】
     */
    // @PreAuthorize("@ss.hasPermi('business:quotation:add')")
    @Log(title = "【新增报价】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuotationAddBO quotationAddBO) {
        tqQuotationService.insertTqQuotation(quotationAddBO);
        return success();
    }

    /**
     * 修改【编辑报价】
     */
    // @PreAuthorize("@ss.hasPermi('business:quotation:edit')")
    @Log(title = "【编辑报价】", businessType = BusinessType.UPDATE)
    @PutMapping
    @DataUpdateCheck(key = "id", tableName = "tq_quotation")
    public AjaxResult edit(@RequestBody QuotationAddBO quotationAddBO) {
        tqQuotationService.updateTqQuotation(quotationAddBO);
        return success();
    }

    /**
     * 【复制版本】
     */
    // @PreAuthorize("@ss.hasPermi('business:quotation:copy')")
    @Log(title = "【复制版本】", businessType = BusinessType.INSERT)
    @PostMapping(value = "/copy")
    public AjaxResult copyVersion(@RequestBody QuotationCopyBO copyBO) {
        tqQuotationService.copyVersions(copyBO);
        return success();
    }

    /**
     * 删除【请填写功能名称】
     */
    // @PreAuthorize("@ss.hasPermi('business:quotation:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tqQuotationService.deleteTqQuotationByIds(ids));
    }

    /**
     * 根据项目id查询报价列表
     */
    @GetMapping("/listByProjectId/{projectId}")
    public AjaxResult listByProjectId(@PathVariable("projectId") Long projectId) {
        return success(tqQuotationService.listByProjectId(projectId));
    }

    // @PreAuthorize("@ss.hasPermi('business:quotation:query')")
    @GetMapping(value = "/price/{quotationId}")
    public AjaxResult getPrice(@PathVariable("quotationId") Long quotationId) {
        return success(tqQuotationService.getPriceInfo(quotationId));
    }

    @GetMapping(value = "/salesDiscount/{quotationId}")
    public AjaxResult getSalesDiscountByQuotationId(@PathVariable("quotationId") Long quotationId) {
        return success(tqQuotationService.getSalesDiscountByQuotationId(quotationId));
    }

    @GetMapping(value = "/salesDiscount/flow")
    public AjaxResult getSalesDiscountByFlow(@RequestParam(name = "flowStatus", required = false) Integer flowStatus) {
        return success(tqQuotationService.getSalesDiscountByFlow(flowStatus));
    }

    @Log(title = "【导出报价】", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{id}")
    public void export(@PathVariable Long id) {
        tqQuotationService.export(id);
    }

    @PostMapping("/uploadQuotationImage")
    public AjaxResult uploadQuotationImage(@RequestBody TbFile tbFile)
    {
        tqQuotationService.update(Wrappers.<TqQuotation>lambdaUpdate()
                .set(TqQuotation::getTradeType, tbFile.getTradeType())
                .set(TqQuotation::getTargetPort, tbFile.getTargetPort())
                .eq(TqQuotation::getId, tbFile.getBusinessId())
        );
        return tbFileService.saveOrUpdateFile(tbFile.getFilePath(), tbFile.getBusinessId(), FileBuzType.SPECIFICATION_CIVIL_IMG.getType())
                ? AjaxResult.success(): AjaxResult.error();
    }

    @GetMapping("/getQuotationImage/{id}")
    public AjaxResult getQuotationImage(@PathVariable Long id)
    {
        TqQuotation quotation = tqQuotationService.getById(id);
        List<TbFile> tbFiles = tbFileService.selectTbFileListByBuzIdAndBuzType(id, FileBuzType.SPECIFICATION_CIVIL_IMG.getType());
        if (CollectionUtils.isEmpty(tbFiles)){
            tbFiles = Lists.newArrayList(new TbFile());
        }
        tbFiles.forEach(tbFile -> {
            tbFile.setTargetPort(quotation.getTargetPort());
            tbFile.setTradeType(quotation.getTradeType());
        });
        return AjaxResult.success(tbFiles);
    }

    /**
     * 导出报价单列表
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/exportList")
    public void export(HttpServletResponse response, QuotationQueryBO queryBO)
    {
        List<QuotationVO> list =  tqQuotationService.selectTqQuotationListAll(queryBO);
        ExcelUtil<QuotationVO> util = new ExcelUtil<QuotationVO>(QuotationVO.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    @GetMapping(value = "/getQuotation4Contract")
    public TableDataInfo getQuotation4Contract(QuotationContractBo quotationContractBo) {
        startPage();
        return  getDataTable(tqQuotationService.getQuotation4Contract(quotationContractBo));
    }

}
