package com.fjhx.jxst.controller.after;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.file.entity.SingDto;
import com.fjhx.file.entity.SingVo;
import com.fjhx.file.service.FileInfoService;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.jxst.entity.after.dto.AfterSalesRecordDto;
import com.fjhx.jxst.service.after.AfterSalesRecordService;
import com.fjhx.mes.entity.production.dto.ProductionTaskDetailDto;
import com.fjhx.mes.entity.production.vo.ProductionTaskDetailVo;
import com.fjhx.mes.service.production.ProductionTaskDetailService;
import com.fjhx.tenant.entity.dict.dto.DictTenantDataSelectDto;
import com.fjhx.tenant.entity.dict.vo.DictTenantDataVo;
import com.fjhx.tenant.mapper.dict.DictTenantDataMapper;
import com.ruoyi.common.annotation.TenantIgnore;
import com.ruoyi.framework.mybatis.holder.TenantHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 售后记录对外开放 前端控制器
 * </p>
 *
 * @author
 * @since 2023-11-21
 */
@TenantIgnore
@RestController
@RequestMapping("/open/afterSales")
public class AfterSalesRecordOpenController {

    @Autowired
    private AfterSalesRecordService afterSalesRecordService;
    @Autowired
    private ProductionTaskDetailService productionTaskDetailService;
    @Autowired
    private FileInfoService fileInfoService;
    @Autowired
    private DictTenantDataMapper dictTenantDataMapper;

    @Value("${tenant.tenantId}")
    private String tenantId;

    /**
     * 获取sn信息
     */
    @PostMapping("/snInfo")
    public ProductionTaskDetailVo snInfo(@RequestBody ProductionTaskDetailDto productionTaskDetailDto) {
        ProductionTaskDetailVo productionTaskDetailVo = productionTaskDetailService.snInfo(productionTaskDetailDto.getProductSn());
        return productionTaskDetailVo;
    }

    /**
     * 售后记录新增
     */
    @PostMapping("/add")
    public void add(@RequestBody AfterSalesRecordDto afterSalesRecordDto) {
        afterSalesRecordDto.setTenantId(tenantId);
        afterSalesRecordService.add(afterSalesRecordDto);
        ObsFileUtil.saveFile(afterSalesRecordDto.getFileList(), afterSalesRecordDto.getId());
    }

    /**
     * 售后类型字典列表
     *
     * @return
     */
    @GetMapping("/afterSalesTypeList")
    public List<DictTenantDataVo> afterSalesTypeList() {
        DynamicDataSourceContextHolder.push(SourceConstant.BASE);
        DictTenantDataSelectDto dto = new DictTenantDataSelectDto();
        dto.setDictCode("after_sales_type");
        dto.setTenantId(tenantId);
        List<DictTenantDataVo> afterSalesType = dictTenantDataMapper.getList(dto);
        DynamicDataSourceContextHolder.poll();
        return afterSalesType;
    }

    /**
     * 获取前端直传签名
     */
    @PostMapping("/fileInfo/getSing")
    public SingVo getSing(@Validated @RequestBody SingDto dto) {
        DynamicDataSourceContextHolder.push(SourceConstant.BASE);
        SingVo sing = fileInfoService.getSing(dto);
        DynamicDataSourceContextHolder.poll();
        return sing;
    }
}
