package com.mortals.xhx.module.api.web;

import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.module.api.RspCode;
import com.mortals.xhx.module.api.common.ListQueryResultRsp;
import com.mortals.xhx.module.api.common.PageInfoReq;
import com.mortals.xhx.module.api.common.PageInfoRsp;
import com.mortals.xhx.module.api.common.QueryApiReq;
import com.mortals.xhx.module.api.model.*;
import com.mortals.xhx.module.supplier.model.SupplierEntity;
import com.mortals.xhx.module.supplier.model.SupplierQuery;
import com.mortals.xhx.module.supplier.service.SupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

/**
 *  供应商接口
 * @author luoshichao
 * @date 2021/3/1 16:35
 */
@Api(tags = "供应商接口")
@RestController
@RequestMapping(value = "api/supplier", method = RequestMethod.POST)
public class SupplierApiController extends BaseApiController{

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private ParamService paramService;


    @ApiOperation("获取供应商列表")
    @RequestMapping("/list")
    public BaseRsp<ListQueryResultRsp<SupplierInfo>> list(@RequestBody QueryApiReq<SupplierGetReq> params) {
        BaseRsp<ListQueryResultRsp<SupplierInfo>> baseRsp = BaseRsp.buildSuccRsp();
        ListQueryResultRsp<SupplierInfo> resultData = new ListQueryResultRsp<>();

        //字典信息
        Map<String, Object> dict = new HashMap<>();
        dict.put("tags", paramService.getParamByFirstOrganize(Constant.Param_SupplierTags));
        dict.put( "nature",  paramService.getParamByFirstOrganize(Constant.Param_PartnerNature));
        resultData.setDict(dict);
        try {
            List<SupplierInfo> resultList = new ArrayList<>();

            SupplierQuery supplierQuery  = new SupplierQuery();
            SupplierGetReq req = params.getQuery();
            if(StringUtils.isNotEmpty(req.getCode())){
                supplierQuery.setCode("%" + req.getCode() +"%"); //支持模糊查询
            }
            if(StringUtils.isNotEmpty(req.getName())){
                supplierQuery.setName("%" + req.getName() +"%"); //支持模糊查询
            }
            if(params.getOrderCols() != null && !params.getOrderCols().isEmpty()){
                supplierQuery.setOrderCols(params.getOrderCols());
            }
            PageInfoReq pageInfoReq = params.getPageInfo();
            if (pageInfoReq != null && pageInfoReq.getPrePageResult() != 0) {//分页
                PageInfo pageInfo = new PageInfo();
                pageInfo.setCurrPage(pageInfoReq.getCurrPage());
                pageInfo.setPrePageResult(pageInfoReq.getPrePageResult());
                Result<SupplierEntity> supplierEntityResult = supplierService.find(supplierQuery, pageInfo, null);
                //分页信息
                PageInfoRsp pageInfoRsp = new PageInfoRsp();
                PropertyUtils.copyProperties(pageInfoRsp, supplierEntityResult.getPageInfo());
                resultData.setPageInfo(pageInfoRsp);

                List<SupplierEntity> list = supplierEntityResult.getList();
                if(!list.isEmpty()){
                    resultList = list.stream().map(item->{
                        SupplierInfo info = new SupplierInfo();
                        BeanUtils.copyProperties(item, info);
                        return info;
                    }).collect(Collectors.toList());
                }
            }else{
                List<SupplierEntity> list = supplierService.find(supplierQuery, null);
                resultList = list.stream().map(item->{
                    SupplierInfo info = new SupplierInfo();
                    BeanUtils.copyProperties(item, info);
                    return info;
                }).collect(Collectors.toList());
            }
            resultData.setList(resultList);
            baseRsp.setData(resultData);
        } catch (Exception e) {
            log.error("", e);
            baseRsp = BaseRsp.buildFailRsp(RspCode.UNKNOWN_ERROR.getValue(), super.convertException(e));
        }
        return baseRsp;
    }

    @ApiOperation("获取物流公司列表")
    @RequestMapping("/deliver/list")
    public BaseRsp<ListQueryResultRsp<SupplierInfo>> getDeliverList(@RequestBody QueryApiReq<SupplierGetReq> params) {
        BaseRsp<ListQueryResultRsp<SupplierInfo>> baseRsp = BaseRsp.buildSuccRsp();
        ListQueryResultRsp<SupplierInfo> resultData = new ListQueryResultRsp<>();

        //字典信息
        Map<String, Object> dict = new HashMap<>();
        dict.put("tags", paramService.getParamByFirstOrganize(Constant.Param_SupplierTags));
        dict.put( "nature",  paramService.getParamByFirstOrganize(Constant.Param_PartnerNature));
        resultData.setDict(dict);
        try {
            List<SupplierInfo> resultList = new ArrayList<>();


            Map<String, Object> queryParam = new HashMap<>();
            SupplierGetReq req = params.getQuery();
            if(StringUtils.isNotEmpty(req.getCode())){
                queryParam.put("code", "%" + req.getCode() +"%");
            }
            if(StringUtils.isNotEmpty(req.getName())){
                queryParam.put("name", "%" + req.getName() +"%" );
            }
            queryParam.put("tagsLike","200002" ); //物流公司

            PageInfoReq pageInfoReq = params.getPageInfo();
            if (pageInfoReq != null && pageInfoReq.getPrePageResult() != 0) {//分页
                PageInfo pageInfo = new PageInfo();
                pageInfo.setCurrPage(pageInfoReq.getCurrPage());
                pageInfo.setPrePageResult(pageInfoReq.getPrePageResult());
                Result<SupplierEntity> supplierEntityResult = supplierService.getListByCondition(queryParam, pageInfo);
                //分页信息
                PageInfoRsp pageInfoRsp = new PageInfoRsp();
                PropertyUtils.copyProperties(pageInfoRsp, supplierEntityResult.getPageInfo());
                resultData.setPageInfo(pageInfoRsp);

                List<SupplierEntity> list = supplierEntityResult.getList();
                if(!list.isEmpty()){
                    resultList = list.stream().map(item->{
                        SupplierInfo info = new SupplierInfo();
                        BeanUtils.copyProperties(item, info);
                        return info;
                    }).collect(Collectors.toList());
                }
            }else{
                PageInfo pageInfo = new PageInfo();
                pageInfo.setCurrPage(1);
                pageInfo.setPrePageResult(-1);
                List<SupplierEntity> list = supplierService.getListByCondition(queryParam, pageInfo).getList();
                resultList = list.stream().map(item->{
                    SupplierInfo info = new SupplierInfo();
                    BeanUtils.copyProperties(item, info);
                    return info;
                }).collect(Collectors.toList());
            }
            resultData.setList(resultList);
            baseRsp.setData(resultData);
        } catch (Exception e) {
            log.error("", e);
            baseRsp = BaseRsp.buildFailRsp(RspCode.UNKNOWN_ERROR.getValue(), super.convertException(e));
        }
        return baseRsp;
    }
}
