package cn.superdesk.uniorder.controller.organisation;

import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.pagination.PageQueryGenericParam;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.organisation.UnifiedSupplierCostConfig;
import cn.superdesk.uniorder.dto.param.organisation.UnifiedSupplierQueryCriteria;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedSupplierCostConfigParam;
import cn.superdesk.uniorder.dubbo.consumer.organisation.UnifiedSupplierCostConfigDubboConsumerService;
import cn.superdesk.uniorder.common.numerical.AnConvertBeanUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @Description: 供应商结算 > 成本核算 特指 上架的商品没有成本价的
 * eg : 得力
 * @Author: linjinyu 补充 修改
 * @CreateDate: 4/28/20 6:20 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 4/28/20 6:20 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "供应商 > 供应商结算-成本核算 特指 上架的商品没有成本价的", tags = {"供应商 > 供应商结算-成本核算 特指 上架的商品没有成本价的"})
@Slf4j
@RestController
@RequestMapping("/usupcostconfig")
public class SysSupplierExtCostConfigController extends BaseController {

	@Autowired
	private UnifiedSupplierCostConfigDubboConsumerService unifiedSupplierCostConfigDubboConsumerService;

	/***
	 * 供应商结算>成本核算 列表分页
	 * @return
	 */
	@RequestMapping(value = "/page", method = RequestMethod.POST)
	@ResponseBody
	public R<List<UnifiedSupplierCostConfig>> page(@RequestBody PageQueryGenericParam<UnifiedSupplierQueryCriteria> criteria) {

		log.info("{}", AnJsonUtils.toJson(criteria));

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

		try {

//			Map<String, Object> params = new HashMap<>();
//
//			params.put("saleName", getString("saleName"));
//			params.put("enabled", 1);

			result = unifiedSupplierCostConfigDubboConsumerService.getDataByPage(getToken(), criteria.getConditions(), getPageNum(), getPageSize());

		} 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;
	}

	/**
	 * 新增成本
	 *
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public R<Object> add(UnifiedSupplierCostConfigParam param) {

		R<Object> result = new R<>();

		try {

			if (param.getSaleId() == null) {
				result.setCode(400);
				result.setSuccess(false);
				result.setMessage("供应商不能为空！");
				return result;
			}

			UnifiedSupplierCostConfig searchData = unifiedSupplierCostConfigDubboConsumerService.findBySaleId(param.getSaleId());

			if (searchData != null) {

				result.setCode(400);
				result.setSuccess(false);
				result.setMessage("供应商已配置！");
				return result;
			}

			UnifiedSupplierCostConfig unifiedSupplierCostConfig = AnConvertBeanUtil.copyBean(param, UnifiedSupplierCostConfig.class);

			if (StringUtils.isNotEmpty(param.getFutureDateStr())) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				unifiedSupplierCostConfig.setFutureDate(sdf.parse(param.getFutureDateStr()));
			}

			Map<String, Object> resMap = unifiedSupplierCostConfigDubboConsumerService.addObj(getToken(), unifiedSupplierCostConfig);

			if (resMap.get("code").toString().equals("1")) {
				result.setSuccess(true);
				result.setData(resMap.get("data"));
			} else {
				result.setSuccess(false);
				result.setMessage(resMap.get("msg").toString());
			}

		} 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;
	}

	/**
	 * 编辑成本
	 *
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public R<Object> update(UnifiedSupplierCostConfigParam param) {

		R<Object> result = new R<>();

		try {

			UnifiedSupplierCostConfig unifiedSupplierCostConfig = AnConvertBeanUtil.copyBean(param, UnifiedSupplierCostConfig.class);

			if (StringUtils.isNotEmpty(param.getFutureDateStr())) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				unifiedSupplierCostConfig.setFutureDate(sdf.parse(param.getFutureDateStr()));
			}

			Map<String, Object> resMap = unifiedSupplierCostConfigDubboConsumerService.updateObj(getToken(), unifiedSupplierCostConfig);

			if (resMap.get("code").toString().equals("1")) {
				result.setSuccess(true);
				result.setData(resMap.get("data"));
			} else {
				result.setSuccess(false);
				result.setMessage(resMap.get("msg").toString());
			}

		} 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;
	}

	/**
	 * 删除成本
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@ResponseBody
	public R<Object> delete(Long id) {

		R<Object> result = new R<>();

		try {

			unifiedSupplierCostConfigDubboConsumerService.delObj(id);
			result.setSuccess(true);

		} 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;
	}

}
