package cn.superdesk.uniorder.controller._thirdpart.szhtxx.electronicInvoice;

import cn.hutool.core.util.StrUtil;
import cn.superdesk.libs.common.json.JsonUtils;
import cn.superdesk.uniorder.common.pagination.PageQueryGenericParam;
import cn.superdesk.uniorder.common.pagination.PageQueryParam;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.rest.response.ResponseResultOuterUtils;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxExtendInfo;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxExtendInfoOpSetRemark2Form;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxExtendInfoParam;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxExtendInfoQueryCriteria;
import cn.superdesk.uniorder.dubbo.consumer.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxExtendInfoDubboConsumerService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description:
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2022-03-17 13:52:19
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022-03-17 13:52:19
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/szhtxxElectronicInvoiceFptxxExtendInfo")
public class SzhtxxElectronicInvoiceFptxxExtendInfoController extends BaseController {

    @Autowired
    private SzhtxxElectronicInvoiceFptxxExtendInfoDubboConsumerService szhtxxElectronicInvoiceFptxxExtendInfoDubboConsumerService;

    @ApiOperation(value = "分页查询 - 默认不用")
    @RequestMapping(value = "listByObscure", method = RequestMethod.POST)
    @ResponseBody
    public R<List<SzhtxxElectronicInvoiceFptxxExtendInfo>> pageQuerySzhtxxElectronicInvoiceFptxxExtendInfo(@RequestBody PageQueryParam param) {

        log.info("pageQuerySzhtxxElectronicInvoiceFptxxExtendInfo:\n{}", JsonUtils.toJson(param));

        R<List<SzhtxxElectronicInvoiceFptxxExtendInfo>> result = new R<>();

        // TODO 参数合法性判定

        result = szhtxxElectronicInvoiceFptxxExtendInfoDubboConsumerService.pageQuery(param.getConditions(), param.getPageNo(), param.getPageSize());

        return result;
    }

    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ResponseBody
    public R<List<SzhtxxElectronicInvoiceFptxxExtendInfo>> pageQueryGenericParamSzhtxxElectronicInvoiceFptxxExtendInfo(@RequestBody PageQueryGenericParam<SzhtxxElectronicInvoiceFptxxExtendInfoQueryCriteria> param) {

        log.info("pageQuerySzhtxxElectronicInvoiceFptxxExtendInfo:\n{}", JsonUtils.toJson(param));

        R<List<SzhtxxElectronicInvoiceFptxxExtendInfo>> result = new R<>();

        // TODO 参数合法性判定

        result = szhtxxElectronicInvoiceFptxxExtendInfoDubboConsumerService.pageQueryGenericParam(param.getConditions());

        return result;
    }

    @ApiOperation(value = "按id查询")
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    @ResponseBody
    public R<List<SzhtxxElectronicInvoiceFptxxExtendInfo>> getById(@PathVariable("id") long id) {

        R<List<SzhtxxElectronicInvoiceFptxxExtendInfo>> result = new R<>();

        // TODO 参数合法性判定

        SzhtxxElectronicInvoiceFptxxExtendInfo entity = szhtxxElectronicInvoiceFptxxExtendInfoDubboConsumerService.findSzhtxxElectronicInvoiceFptxxExtendInfoById(id);

        List<SzhtxxElectronicInvoiceFptxxExtendInfo> data = new ArrayList<>();
        data.add(entity);

        result.setData(data);

        return result;
    }

    @ApiOperation(value = "新增")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public R<String> addSzhtxxElectronicInvoiceFptxxExtendInfo(@RequestBody SzhtxxElectronicInvoiceFptxxExtendInfoParam param) {

        log.info("addSzhtxxElectronicInvoiceFptxxExtendInfo:\n{}", JsonUtils.toJson(param));


        // TODO 参数合法性判定

        szhtxxElectronicInvoiceFptxxExtendInfoDubboConsumerService.addSzhtxxElectronicInvoiceFptxxExtendInfo(param);

        return new R<>("200", "操作成功", true);
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public R<String> updateSzhtxxElectronicInvoiceFptxxExtendInfo(@RequestBody SzhtxxElectronicInvoiceFptxxExtendInfoParam param) {

        log.info("updateSzhtxxElectronicInvoiceFptxxExtendInfo:\n{}", JsonUtils.toJson(param));

        szhtxxElectronicInvoiceFptxxExtendInfoDubboConsumerService.updateSzhtxxElectronicInvoiceFptxxExtendInfo(param);

        return new R<>("200", "操作成功", true);
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public R<String> deleteSzhtxxElectronicInvoiceFptxxExtendInfo(@PathVariable("id") long id) {

        // TODO 参数合法性判定

        szhtxxElectronicInvoiceFptxxExtendInfoDubboConsumerService.deleteSzhtxxElectronicInvoiceFptxxExtendInfo(id);

        return new R<>("200", "操作成功", true);
    }

    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "delete_ids", method = RequestMethod.POST)
    @ResponseBody
    public R<String> deleteIdsSzhtxxElectronicInvoiceFptxxExtendInfo(@RequestParam(value = "ids") long[] ids) {

        log.info(JsonUtils.toJson(ids));

        for (long id : ids) {
            szhtxxElectronicInvoiceFptxxExtendInfoDubboConsumerService.deleteSzhtxxElectronicInvoiceFptxxExtendInfo(id);
        }

        return new R<>("200", "操作成功", true);

    }


    @ApiOperation(value = "op后台备注2")
    @RequestMapping(value = "/opSetRemark2", method = RequestMethod.POST)
    @ResponseBody
    public R<String> opSetRemark2(@RequestBody SzhtxxElectronicInvoiceFptxxExtendInfoOpSetRemark2Form form) {

        if (StrUtil.isBlank(form.getFpqqlsh()) || StrUtil.isBlank(form.getRemark())) {
            return ResponseResultOuterUtils.createErrorResult("500", "异常,参数错误");
        }

        int ret = szhtxxElectronicInvoiceFptxxExtendInfoDubboConsumerService.saveOrUpdate(
                SzhtxxElectronicInvoiceFptxxExtendInfo
                        .builder()
                        .fpqqlsh(form.getFpqqlsh())
                        .remark2(form.getRemark())
                        .build(),
                getToken()
        );

        if (ret == 0) {
            return ResponseResultOuterUtils.createErrorResult("500", "异常,后台备注失败");
        } else {
            return ResponseResultOuterUtils.createSuccesResult("后台备注成功");
        }
    }


}
