package com.izouma.zhirongip.web.supply;

import cn.hutool.core.convert.Convert;
import com.google.gson.JsonArray;
import com.izouma.zhirongip.domain.supply.TechnologyProduct;
import com.izouma.zhirongip.dto.PageQuery;
import com.izouma.zhirongip.enums.ApplyStatus;
import com.izouma.zhirongip.exception.BusinessException;
import com.izouma.zhirongip.repo.SettingRepo;
import com.izouma.zhirongip.repo.supply.TechnologyProductRepo;
import com.izouma.zhirongip.service.supply.TechnologyProductService;
import com.izouma.zhirongip.utils.ObjUtils;
import com.izouma.zhirongip.utils.SecurityUtils;
import com.izouma.zhirongip.utils.excel.ExcelUtils;
import com.izouma.zhirongip.web.BaseController;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/technologyProduct")
@AllArgsConstructor
public class TechnologyProductController extends BaseController {
    private final TechnologyProductService technologyProductService;
    private final TechnologyProductRepo    technologyProductRepo;
    private final SettingRepo              settingRepo;


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public TechnologyProduct save(@RequestBody TechnologyProduct record) {
        if (record.getId() != null) {
            TechnologyProduct orig = technologyProductRepo.findById(record.getId())
                    .orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return technologyProductRepo.save(orig);
        }
        record.setStatus(ApplyStatus.PENDING);
        record.setUserId(SecurityUtils.getAuthenticatedUser().getId());
        return technologyProductRepo.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<TechnologyProduct> all(@RequestBody PageQuery pageQuery) {
        Map<String, Object> query = pageQuery.getQuery();
        Object moneyObj = query.get("money");
        String money = Convert.convert(String.class, moneyObj);

        return technologyProductService.all(pageQuery, money);
    }

    @GetMapping("/get/{id}")
    public TechnologyProduct get(@PathVariable Long id) {
        TechnologyProduct record = technologyProductRepo.findById(id).orElseThrow(new BusinessException("无记录"));
        settingRepo.findById(record.getField())
                .ifPresent(setting -> record.setFieldName(setting.getName()));
        if(record.getTradingMethodId()!=null){
            settingRepo.findById(record.getTradingMethodId())
                    .ifPresent(setting -> record.setTradingMethod(setting.getName()));
        }
        return record;
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        technologyProductRepo.softDelete(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<TechnologyProduct> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("审核")
    @PostMapping("/audit")
    public void audit(@RequestParam Long id, @RequestParam boolean pass, String remark) {
        technologyProductService.audit(id, pass, remark);
    }

    @GetMapping("/top")
    public List<TechnologyProduct> top () {
        return technologyProductRepo.top();
    }

}

