package com.sifang.modules.sunshine.partnerManagement.web;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.setting.service.MemberService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.partnerManagement.entity.CrmPartner;
import com.sifang.modules.sunshine.partnerManagement.form.AddPartnerReq;
import com.sifang.modules.sunshine.partnerManagement.service.PartnerService;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.dao.UserDao;
import com.sifang.modules.sys.util.GuardUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/partnerManagement")
public class PartnerController extends BaseExceptionController {
    Logger logger = LoggerFactory.getLogger(PartnerController.class);
    @Autowired
    private PartnerService partnerService;
    @Autowired
    private UserDao userDao;
    @Autowired
    private MemberService memberService;

    @Autowired
    AddressService addressService;
    /**
     * 合作商列表页面
     */
    @RequestMapping(value = "toPartnerPage")
    public String toPartnerPage(Model model) {
        model.addAttribute("projects", memberService.getProjectLists());
        return "modules/yangguang/systemSetting/partner/partnerPage";
    }

    /**
     * 合作商列表数据
     */
    @ResponseBody
    @RequestMapping(value = "ajaxForPartnerData")
    public Object ajaxForPartnerData(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        partnerService.getPartnerPage(page, map);
        return page.renderJson();
    }

    @RequestMapping(value = "toPartnerForm")
    public String toAddPartnerForm(HttpServletRequest request, Model model) {
        List<Record> province = addressService.getProvinces();
        model.addAttribute("province", province);
        String id = request.getParameter("id");
        if (StrKit.notBlank(id)) {
            CrmPartner partner = partnerService.getById(id);
            User user = userDao.get(partner.getUserId());
            model.addAttribute("partner", partner);
            model.addAttribute("user", user);
            List<Record> city = AddressUtils.getCityByProName(partner.getProvince());
            List<Record> area = AddressUtils.getAreaByCityName(partner.getCity());
            model.addAttribute("city", city);
            model.addAttribute("area", area);
        }
        model.addAttribute("projects", memberService.getProjectLists());
        return "modules/yangguang/systemSetting/partner/partnerForm";
    }

    /**
     * 新增合作商
     */
    @ResponseBody
    @RequestMapping(value = "addPartner")
    public Result addPartner(@Valid AddPartnerReq req, BindingResult bindingResult) {
        logger.info("into addPartner, req={}", req);
        if (bindingResult.hasErrors()) {
            return errRet(bindingResult);
        }
        if(StringUtil.isNotBlank(req.getId())){
            partnerService.editPartner(req);
        }else{
            partnerService.addPartner(req);
        }
        return Result.ok();
    }

    /**
     * 编辑合作商
     */
    @ResponseBody
    @RequestMapping(value = "editPartner")
    public Result editPartner(@Valid AddPartnerReq req, BindingResult bindingResult) {
        logger.info("into editPartner, req={}", req);
        if (bindingResult.hasErrors()) {
            return errRet(bindingResult);
        }
        partnerService.editPartner(req);
        return Result.ok();
    }

    /**
     * 删除合作商
     */
    @ResponseBody
    @RequestMapping(value = "deletePartner")
    public Result deletePartner(String id) {
        logger.info("into deletePartner, id={}", id);
        GuardUtils.notBlank(id, "id不能为空");
        partnerService.deletePartner(id);
        return Result.ok();
    }

    @ResponseBody
    @RequestMapping(value = "getPartnerByIds")
    public Result getPartnerByIds(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        List<Record> records = partnerService.getPartnerByIds(ids);
        return Result.success(records);
    }

    /**
     * 华融资方 设置合同的签署模式 上传<<==>>在线签署
     */
    @ResponseBody
    @RequestMapping(value = "changePatternType")
    public Result changePatternType(HttpServletRequest request){
        String partnerId=request.getParameter("partnerId"); //资方id
        String newPattern=request.getParameter("newPattern"); //转换后的签约模式
        partnerService.changePatternType(partnerId,newPattern);
        return Result.ok();
    }
}
