package cn.superdesk.uniorder.controller.organisation;

import cn.superdesk.libs.common.json.JsonUtils;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.pagination.PageQueryParam;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.organisation.UnifiedSupplier;
import cn.superdesk.uniorder.dto.param.organisation.SysSupplierQueryParam;
import cn.superdesk.uniorder.dto.param.organisation.UnifiedSupplierParam;
import cn.superdesk.uniorder.dto.param.organisation.UnifiedSupplierQueryCriteria;
import cn.superdesk.uniorder.dto.vo.organisation.UnifiedSupplierVo;
import cn.superdesk.uniorder.dto.vo.sys.SysSelectVo;
import cn.superdesk.uniorder.dto.vo.sys.SysSupplierVo;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysSupplierDubboConsumerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 供应商-聚合数据-非统一订单产生
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2021-05-14 12:51:18
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-05-14 12:51:18
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "供应商-聚合数据-非统一订单产生", tags = {"供应商-聚合数据-非统一订单产生"})
@Slf4j
@RestController
@RequestMapping("/unifiedSupplier")
public class SysSupplierController extends BaseController {

    @Autowired
    private SysSupplierDubboConsumerService unifiedSupplierDubboConsumerService;

    /**
     * http://192.168.6.223:8089/web/accounting/unified/supplier/notyet/page
     * http://192.168.6.223:8089/web/unifiedSupplier/getSupplierList
     *
     * @return
     */
    @ApiOperation(value = "供应商结算-成本核算-商户列表(下拉)(供应商下拉)")
    @RequestMapping(value = "/getSupplierList", method = RequestMethod.POST)
    @ResponseBody
    public R<List<UnifiedSupplierVo>> pageQueryUnifiedSuppliers(
            @RequestBody UnifiedSupplierQueryCriteria queryCriteria) {

        log.info("getSupplierList \n{}", JsonUtils.toJson(queryCriteria));

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

        try {

            /**
             * orderByClause
             * us.update_time desc ---- 更新时间 倒序
             * us.id asc ---- 结算中心Id 升序
             * us.sale_id asc ---- 第三方商户Id 升序
             */

            result = unifiedSupplierDubboConsumerService
                    .pageQuery(
                            queryCriteria,
                            getToken()
                    );

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

        return result;
    }

    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ResponseBody
    public R<List<UnifiedSupplier>> pageQueryUnifiedSuppliers(@RequestBody PageQueryParam param) {

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

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

        // TODO 参数合法性判定

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

        return result;
    }

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

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

        // TODO 参数合法性判定

        UnifiedSupplier entity = unifiedSupplierDubboConsumerService.findUnifiedSupplierById(id);

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

        result.setData(data);

        return result;
    }

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

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


        // TODO 参数合法性判定

        unifiedSupplierDubboConsumerService.addUnifiedSupplier(param, getToken());

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

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

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

        unifiedSupplierDubboConsumerService.updateUnifiedSupplier(param, getToken());

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

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

        // TODO 参数合法性判定

        unifiedSupplierDubboConsumerService.deleteUnifiedSupplier(id, getToken());

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

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

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

        for (long id : ids) {
            unifiedSupplierDubboConsumerService.deleteUnifiedSupplier(id, getToken());
        }

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

    }



    @ApiOperation(value = "供应商数据列表", notes = "供应商数据列表")
    @RequestMapping(value = "findSupplierList", method = RequestMethod.POST)
    @ResponseBody
    public R<List<SysSupplierVo>> findSupplierList(@RequestBody @Validated SysSupplierQueryParam param) {
        log.info("SysSupplierController -> findSupplierList:{}", AnJsonUtils.toJson(param));
        R<List<SysSupplierVo>> result = new R<>();
        result = unifiedSupplierDubboConsumerService.findSupplierList(param);
        return result;
    }


    @ApiOperation(value = "查询供应商下拉列表", notes = "供应商")
    @PostMapping("/selectList")
    @ResponseBody
    public R<List<SysSelectVo>> selectList() {
        R<List<SysSelectVo>> result = new R<>();
        Map<String,Object> params = new HashMap<>();
        result.setData(unifiedSupplierDubboConsumerService.selectList(params));
        return result;
    }

}
