package com.style24.admin.biz.web;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gagaframework.excel.GagaExcelUtil;
import com.gagaframework.web.parameter.GagaMap;
import com.gagaframework.web.rest.server.GagaResponse;
import com.gagaframework.web.util.GagaFileUtil;
import com.style24.admin.biz.service.TsaOcmService;
import com.style24.admin.biz.service.TsaRendererService;
import com.style24.admin.support.controller.TsaBaseController;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.core.biz.service.TscOrderOcmService;
import com.style24.core.support.env.TscConstants;
import com.style24.core.support.message.TscMessageByLocale;
import com.style24.persistence.domain.Extmall;
import com.style24.persistence.domain.ExtmallNoti;
import com.style24.persistence.domain.ExtmallOrder;
import com.style24.persistence.domain.ExtmallOrigin;
import com.style24.persistence.domain.ExtmallPriceSync;

import lombok.extern.slf4j.Slf4j;

/**
 * 영업망관리 Controller
 *
 * @author gagamel
 * @since 2020. 11. 5
 */
@Controller
@RequestMapping("/ocm")
@Slf4j
public class TsaOcmController extends TsaBaseController {

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private Environment env;

	@Autowired
	private TsaOcmService ocmService;

	@Autowired
	private TscOrderOcmService orderOcmService;

	@Autowired
	private TsaRendererService rendererService;

	private static final int EXCEL_ROW_COUNT = 500;

	/**
	 * 제휴몰관리 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 5
	 */
	@GetMapping("/extmall/form")
	public ModelAndView extmallForm() {
		ModelAndView mav = new ModelAndView();

		// 제휴몰벤더
		mav.addObject("vendorList", rendererService.getCommonCodeList("G003", "Y", TsaSession.getInfo().getSupplyCompCd()));

		// 공급업체
		mav.addObject("supplyCompList", rendererService.getSupplyCompanyList(TsaSession.getInfo().getSupplyCompCd()));

		mav.setViewName("ocm/ExtmallForm");

		return mav;
	}

	/**
	 * 제휴몰 목록
	 * @param extmall - 제휴몰 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 5
	 */
	@PostMapping("/extmall/list")
	@ResponseBody
	public Collection<Extmall> getExtmallList(@RequestBody Extmall extmall) {
		return ocmService.getExtmallList(extmall);
	}

	/**
	 * 제휴몰 목록 화면(팝업)
	 * @param extmall - 제휴몰 정보
	 * @return
	 * @author xodud1202
	 * @since 2020. 12. 28
	 */
	@GetMapping("/extmall/search/form")
	public ModelAndView extmallSearchForm(Extmall extmall) {
		ModelAndView mav = new ModelAndView();

		// 상품상태
		mav.addObject("extmallGbList", rendererService.getCommonCodeList("G003", "Y"));

		mav.addObject("params", extmall);
		mav.setViewName("ocm/ExtmallSearchForm");
		return mav;
	}

	/**
	 * 제휴몰 등록/수정 처리
	 * @param extmall - 제휴몰 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 5
	 */
	@PostMapping("/extmall/save")
	@ResponseBody
	public GagaResponse saveExtmall(@RequestBody Extmall extmall) {
		ocmService.saveExtmall(extmall);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 제휴몰고시정보관리 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 6
	 */
	@GetMapping("/extmall/notiinfo/form")
	public ModelAndView extmallNotiinfoForm() {
		ModelAndView mav = new ModelAndView();

		// 제휴몰벤더
		mav.addObject("vendorList", rendererService.getCommonCodeList("G003", "Y", TsaSession.getInfo().getSupplyCompCd()));

		mav.setViewName("ocm/ExtmallNotiinfoForm");

		return mav;
	}

	/**
	 * 제휴몰고시정보 목록
	 * @param extmallNoti - 제휴몰고시정보
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 6
	 */
	@PostMapping("/extmall/notiinfo/list")
	@ResponseBody
	public Collection<ExtmallNoti> getExtmallNotiinfoList(@RequestBody ExtmallNoti extmallNoti) {
		return ocmService.getExtmallNotiinfoList(extmallNoti);
	}

	/**
	 * 제휴몰원산지 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 12
	 */
	@GetMapping("/extmall/origin/form")
	public ModelAndView extmallOriginForm() {
		ModelAndView mav = new ModelAndView();

		// 제휴몰벤더
		mav.addObject("vendorList", rendererService.getCommonCodeList("G003", "Y", TsaSession.getInfo().getSupplyCompCd()));

		mav.setViewName("ocm/ExtmallOriginForm");

		return mav;
	}

	/**
	 * 제휴몰원산지 목록
	 * @param extmallOrigin - 제휴몰원산지 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 12
	 */
	@PostMapping("/extmall/origin/list")
	@ResponseBody
	public Collection<ExtmallOrigin> getExtmallOriginList(@RequestBody ExtmallOrigin extmallOrigin) {
		return ocmService.getExtmallOriginList(extmallOrigin);
	}

	/**
	 * 제휴몰가격연계관리 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 7
	 */
	@GetMapping("/extmall/price/sync/form")
	public ModelAndView extmallPriceSyncForm() {
		ModelAndView mav = new ModelAndView();

		// 제휴몰벤더
		mav.addObject("vendorList", rendererService.getCommonCodeList("G003", "Y", TsaSession.getInfo().getSupplyCompCd()));

		// 공급업체
		mav.addObject("supplyCompList", rendererService.getSupplyCompanyList(TsaSession.getInfo().getSupplyCompCd()));

		mav.setViewName("ocm/ExtmallPriceSyncForm");

		return mav;
	}

	/**
	 * 제휴몰가격연계 목록
	 * @param extmallPriceSync - 제휴몰가격연계 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 7
	 */
	@PostMapping("/extmall/price/sync/list")
	@ResponseBody
	public Collection<ExtmallPriceSync> getExtmallPriceSyncList(@RequestBody ExtmallPriceSync extmallPriceSync) {
		return ocmService.getExtmallPriceSyncList(extmallPriceSync);
	}

	/**
	 * 제휴몰가격연계 등록/수정 처리
	 * @param extmallPriceSyncList - 제휴몰가격연계 목록
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 7
	 */
	@PostMapping("/extmall/price/sync/save")
	@ResponseBody
	public GagaResponse saveExtmallPriceSync(@RequestBody Collection<ExtmallPriceSync> extmallPriceSyncList) {
		if (extmallPriceSyncList == null || extmallPriceSyncList.isEmpty()) {
			throw new IllegalStateException(message.getMessage("FAIL_1001"));
		}

		ocmService.saveExtmallPriceSync(extmallPriceSyncList);
		return super.ok(message.getMessage("SUCC_0001"));
	}







	/**
	 * 제휴몰주문업로드 화면
	 * @param
	 * @return
	 * @author jmh
	 * @since 2021. 06. 15
	 */
	@GetMapping("/extmall/order/upload/form")
	public ModelAndView extmallOrderUploadForm() {
		ModelAndView mav = new ModelAndView();

		mav.setViewName("ocm/ExtmallOrderRegisterForm");

		return mav;
	}


	/**
	 * 제휴몰주문업로드 엑셀 업로드
	 *
	 * @param extmallOrder - 외부몰주문정보
	 * @return GagaResponse
	 * @throws Exception -exception
	 * @author jmh
	 * @since 2021. 06. 17
	 */
	@PostMapping("/extmallorder/excelupload/save")
	@ResponseBody
	public GagaResponse saveExtmallorderExcelupload(@RequestBody ExtmallOrder extmallOrder) throws Exception {

		String targetPath = env.getProperty("upload.excel.target.path") + "/excel/";
		Collection<GagaMap> ecxelGoodsList = new ArrayList<>();

		// 외부몰주문등록
		if ("createExtmallOrder".equals(extmallOrder.getProcJob())) {
			// DB 처리 시 사용되는 파라미터명(셀명) 설정
			String[] extMallOrderNames = {"ordDt", "extmallId", "extmallOrderId", "agentOrderId", "extmallProdId", "goodsCd", "sku", "optCd", "currPrice", "ordQty", "delvFee", "ordNm", "ordTelno", "ordPhnno", "ordEmail", "recipNm", "recipTelno", "recipPhnno", "recipZipcode", "recipBaseAddr", "recipDtlAddr", "delvMemo"};

			ecxelGoodsList = GagaExcelUtil.getList(GagaFileUtil.getConcatenationPath(targetPath, extmallOrder.getExcelFileNm()), 0, extMallOrderNames, 0);
		}

		if (ecxelGoodsList != null && ecxelGoodsList.size() > EXCEL_ROW_COUNT) {
			// 파일 삭제
			GagaFileUtil.deleteFile(GagaFileUtil.getConcatenationPath(targetPath, extmallOrder.getExcelFileNm()));
			throw new IllegalStateException("엑셀 파일의 건수를 " +EXCEL_ROW_COUNT + "건 이하로 사용하세요.");
		}

		int userNo = TsaSession.getInfo().getUserNo();
		orderOcmService.createExtmallOrderExcelupload(ecxelGoodsList, extmallOrder.getExcelFileNm(), userNo);

		// 파일 삭제
		GagaFileUtil.deleteFile(GagaFileUtil.getConcatenationPath(targetPath, extmallOrder.getExcelFileNm()));

		return super.ok(message.getMessage("SUCC_0007"));
	}

	/**
	 * 제휴몰주문업로드 업로드 후 목록
	 *
	 * @return Collection<ExtmallOrder>
	 * @throws Exception -exception
	 * @author jmh
	 * @since 2021. 06. 17
	 */
	@PostMapping("/extmallorder/upload/tmp/list")
	@ResponseBody
	public Collection<ExtmallOrder> getExtmallOrderUploadTmpList() throws Exception {
		int userNo = TsaSession.getInfo().getUserNo();
		return orderOcmService.getExtmallOrderUploadTmpList(userNo);
	}

	/**
	 * 제휴몰주문업로드 업로드 후 저장 처리
	 *
	 * @param extmallOrder - 외부몰주문업로드 정보
	 * @return GagaResponse
	 * @throws Exception -exception
	 * @author jmh
	 * @since 2021. 06. 17
	 */
	@PostMapping("/extmallorder/save")
	@ResponseBody
	public GagaResponse saveExtmallorder(@RequestBody ExtmallOrder extmallOrder) {
		extmallOrder.setRegNo(TsaSession.getInfo().getUserNo());
		extmallOrder.setVendorId(TscConstants.VendorId.SHOPLINKER.value());

		String resultMsg = orderOcmService.saveExtmallorder(extmallOrder);
		return super.ok(resultMsg);
	}

	/**
	 * 제휴몰주문업로드 업로드 후 목록(결과확인)
	 *
	 * @return Collection<ExtmallOrder>
	 * @throws Exception -exception
	 * @author jmh
	 * @since 2021. 06. 17
	 */
	@PostMapping("/extmallorder/upload/second/list")
	@ResponseBody
	public Collection<ExtmallOrder> getExtmallOrderUploadSecondList() {
		ExtmallOrder extmallOrder = new ExtmallOrder();
		extmallOrder.setRegNo(TsaSession.getInfo().getUserNo());

		return orderOcmService.getExtmallOrderUploadSecondList(extmallOrder);
	}


}
