package com.style24.admin.biz.web;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.style24.admin.biz.service.TsaBusinessService;
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.support.message.TscMessageByLocale;
import com.style24.persistence.domain.Aflink;
import com.style24.persistence.domain.DeliveryLoc;
import com.style24.persistence.domain.SellStore;
import com.style24.persistence.domain.ShipCompany;
import com.style24.persistence.domain.SupplyCompany;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.rest.server.GagaResponse;

/**
 * 영업관리 Controller
 *
 * @author jaewonHo
 * @since 2020. 10. 14
 */
@Controller
@RequestMapping("/business")
@Slf4j
public class TsaBusinessController extends TsaBaseController {

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private TsaBusinessService businessService;

	@Autowired
	private TsaRendererService rendererService;

	/**
	 * 공급업체관리 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 14
	 */
	@GetMapping("/supply/company/form")
	public ModelAndView supplyCompanyForm() {
		ModelAndView mav = new ModelAndView();

		// 입점상태
		mav.addObject("supplyStatList", rendererService.getCommonCodeList("G010"));

		// 유통구분
		mav.addObject("distributionGbList", rendererService.getCommonCodeList("G065"));

		// 은행
		mav.addObject("bankList", rendererService.getCommonCodeList("G940"));

		mav.setViewName("business/SupplyCompanyForm");

		return mav;
	}

	/**
	 * 공급업체관리 목록
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 14
	 */
	@PostMapping("/supply/company/list")
	@ResponseBody
	public Collection<SupplyCompany> getCompanyList(@RequestBody SupplyCompany supplyComp) {
		return businessService.getSupplyCompanyList(supplyComp);
	}

	/**
	 * 공급업체관리 저장 처리
	 * @param supplyComp - 공급업체 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 14
	 */
	@PostMapping("/supply/company/save")
	@ResponseBody
	public GagaResponse saveSupplyInfo(@RequestBody SupplyCompany supplyComp) {
		supplyComp.setRegNo(TsaSession.getInfo().getUserNo());
		supplyComp.setUpdNo(TsaSession.getInfo().getUserNo());

		businessService.saveSupplyCompany(supplyComp);

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

	/**
	 * 공급업체출고처관리 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 16
	 **/
	@GetMapping("/supply/company/delvloc/form")
	public ModelAndView deliveryLocForm() {
		ModelAndView mav = new ModelAndView();

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

		// 출고처분류
		mav.addObject("delvLocClsfList", rendererService.getCommonCodeList("G024"));

		// 송장출력형태
		mav.addObject("invoicePrintTypeList", rendererService.getCommonCodeList("G025"));

		mav.setViewName("business/DeliveryLocForm");

		return mav;
	}

	/**
	 * 공급업체출고처 목록
	 * @param delvLoc - 출고처 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 16
	 **/
	@PostMapping("/supply/company/delvloc/list")
	@ResponseBody
	public Collection<DeliveryLoc> getDeliveryLocList(@RequestBody DeliveryLoc delvLoc) {
		return businessService.getDeliveryLocList(delvLoc);
	}

	/**
	 * 공급업체출고처 저장 처리
	 * @param delvLoc - 출고처 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 16
	 */
	@PostMapping("/supply/company/delvloc/save")
	@ResponseBody
	public GagaResponse saveDeliveryLoc(@RequestBody DeliveryLoc delvLoc) {
		delvLoc.setRegNo(TsaSession.getInfo().getUserNo());
		delvLoc.setUpdNo(TsaSession.getInfo().getUserNo());
		businessService.saveDeliveryLoc(delvLoc);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 판매매장관리 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 20
	 */
	@GetMapping("/sellstore/form")
	public ModelAndView sellStoreForm() {
		ModelAndView mav = new ModelAndView();

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

		mav.setViewName("business/SellStoreForm");

		return mav;
	}

	/**
	 * 판매매장 목록
	 * @param sellStore - 판매매장 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 20
	 */
	@PostMapping("/sellstore/list")
	@ResponseBody
	public Collection<SellStore> getSellStoreList(@RequestBody SellStore sellStore) {
		return businessService.getSellStoreList(sellStore);
	}

	/**
	 * 판매매장 목록 저장 처리
	 * @param sellStoreList - 판매매장 목록
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 20
	 */
	@PostMapping("/sellstore/list/save")
	@ResponseBody
	public GagaResponse saveSellStoreList(@RequestBody Collection<SellStore> sellStoreList) {
		if (sellStoreList == null || sellStoreList.isEmpty()) {
			throw new IllegalStateException(message.getMessage("FAIL_1001"));
		}

		businessService.saveSellStoreList(sellStoreList);

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

	/**
	 * 제휴채널관리
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 20
	 */
	@GetMapping("/aflink/form")
	public ModelAndView aflinkForm() {
		ModelAndView mav = new ModelAndView();

		// 제휴채널 콤보박스 목록
		mav.addObject("afChannelList", rendererService.getCommonCodeList("G053"));

		mav.setViewName("business/AflinkForm");

		return mav;
	}

	/**
	 * 제휴채널 목록
	 * @param afChannel - 제휴채널
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 20
	 */
	@PostMapping("/aflink/list")
	@ResponseBody
	public Collection<Aflink> getAflinkList(@RequestParam(value = "afChannel", required = false) String afChannel) {
		Aflink aflink = new Aflink();
		aflink.setAfChannel(afChannel);
		return businessService.getAflinkList(aflink);
	}

	/**
	 * 제휴링크 저장 처리
	 * @param aflink - 제휴링크 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 20
	 */
	@PostMapping("/aflink/save")
	@ResponseBody
	public GagaResponse saveAflink(@RequestBody Aflink aflink) {
		businessService.saveAflink(aflink);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 제휴링크 목록 사용안함 처리
	 * @param aflinkList - 제휴링크 목록
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 20
	 */
	@PostMapping("/aflink/list/delete")
	@ResponseBody
	public GagaResponse deleteAflinkList(@RequestBody Collection<Aflink> aflinkList) {
		if (aflinkList == null || aflinkList.isEmpty()) {
			throw new IllegalStateException(message.getMessage("FAIL_1001"));
		}

		businessService.deleteAflinkList(aflinkList);
		return super.ok(message.getMessage("SUCC_0003"));
	}

	/**
	 * 배송업체관리 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 20
	 */
	@GetMapping("/ship/company/form")
	public ModelAndView deliveryCompanyForm() {
		ModelAndView mav = new ModelAndView();

		mav.setViewName("business/ShipCompanyForm");

		return mav;
	}

	/**
	 * 배송업체 목록
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 20
	 */
	@GetMapping("/ship/company/list")
	@ResponseBody
	public Collection<ShipCompany> getShipCompanyList() {
		return businessService.getShipCompanyList();
	}

	/**
	 * 배송업체 목록 저장
	 * @param shipCompList - 배송업체 목록
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 20
	 */
	@PostMapping("/ship/company/list/save")
	@ResponseBody
	public GagaResponse saveShipCompanyList(@RequestBody Collection<ShipCompany> shipCompList) {
		if (shipCompList == null || shipCompList.isEmpty()) {
			throw new IllegalStateException(message.getMessage("FAIL_1001"));
		}

		businessService.saveShipCompanyList(shipCompList);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 배송업체 목록 삭제
	 * @param shipCompList - 배송업체 목록
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 20
	 */
	@PostMapping("/ship/company/list/delete")
	@ResponseBody
	public GagaResponse deleteShipCompanyList(@RequestBody Collection<ShipCompany> shipCompList) {
		if (shipCompList == null || shipCompList.isEmpty()) {
			throw new IllegalStateException(message.getMessage("FAIL_1004"));
		}

		businessService.deleteShipCompanyList(shipCompList);
		return super.ok(message.getMessage("SUCC_0003"));
	}

}
